对象与对象

时间:2016-01-25 01:01:56

标签: javascript

function Person(idn, dep, nam, age, gen, aut) {
    this.idn = idn; //IDNumber
    this.dep = dep; //Department
    this.nam = nam; //Name
    this.age = age; //Age
    this.gen = gen; //Gender
    this.aut = aut;
  } //Auto

function Car(make, modl, year, lice, colo, mile, ownr) {
    this.make = make; //MakeOfTheCar
    this.modl = modl; //ModelOfTheCar
    this.year = year; //Year
    this.lice = lice; //Licence
    this.colo = colo; //ColorOfTheCar
    this.mile = mile; //Mileage
    this.ownr = ownr;
  } //Owner

var p01 = new Person(3475, 1, 'Rand,McKinnon', 33, 'M', c01);
var p02 = new Person(7608, 2, 'Ken,Jones', 39, 'M', c02);
var p03 = new Person(1957, 3, 'Vladi,Orlov', 58, 'M', c03);

var c01 = new Car('Eagle', 'Talon,TSi', 1993, 'BP456H46', 'red', 201, p01);
var c02 = new Car('Nissan', '300,ZX', 'J001', 1992, 'blue', 244, p02);
var c03 = new Car('Toyota', 'Avalon,XLS', '6HPR64W', 2000, 'black', 118, p03);

document.write('Car:c03.make=', c03.make, '|', c03.colo, '|', c03.ownr.nam, '<br>');
document.write('Person:p03.idn=', p03.idn, '|', p03.nam, '|', p03.gen, '|', p03.aut, '<br>');

  

最后一个参数(p03.aut):未定义

需要你的帮助来弄清楚如何使它发挥作用。

1 个答案:

答案 0 :(得分:2)

当你执行这行代码时:

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

#include "Queue.h"
void print(string s1, string q1)
{
    cout << s1 << "  ";
    cout << q1 << endl;
}
int main()
{
    bool isPalin= false;
    string word;
    //string temp;
    Stack s1;
    Queue q1;
    void print(string, string);

    cout<< " Enter a word you would like to see if it is a palindrome: \n";
    getline(cin, word);

    cout<< "The word you entered is: "<< word<< endl;
    for ( int i = 0; i<=(word.size()-1); i++)
    {
        string temp(word, i, 1);
        s1.push(temp);

    }

    for (int i = 0; i<=(word.size()-1); i++)
    {
        string temp(word, i, 1);

        q1.enqueue(temp);

    }
    while (!s1.empty())
    {
        print(s1.top(), q1.front());

        if( s1.top() != q1.front())
        {
             isPalin = true;
            s1.pop();
            q1.dequeue();
        }
        cout<< " Lets check if this word is a palindrome" << boolalpha<< isPalin<<endl;
    }


Output: 
 Enter a word you would like to see if it is a palindrome: 
word
The word you entered is: word
d  w
 Lets check if this word is a palindrometrue
r  o
 Lets check if this word is a palindrometrue
o  r
 Lets check if this word is a palindrometrue
w  d
 Lets check if this word is a palindrometrue

var p03 = new Person(1957, 3, 'Vladi,Orlov', 58, 'M', c03); 还没有价值。因此,您在那里传递c03值,这就是undefined中存储的值。当您稍后检索该值时,它将检索您之前放在那里的p03.aut值。

由于你有鸡和蛋的问题(两个对象都可以在引用彼此之前调用它们的构造函数,因为其中一个必须首先创建),你将在对象上设置其中一个参数在两者都被构造之后(例如,不在构造函数中)。

例如,你可以这样做:

undefined