如何从类中调用此属性?

时间:2017-03-14 20:28:26

标签: javascript typescript

我一直试图调用该类的属性,但我得到了一个Typeerror,我想从飞行员那里获得名字导航并打印出来

这是我的 Pilot.ts

namespace tsprojectnamespace {
import Vehicle = tsprojectnamespace.Vehicle;
import Pilotinterface = tsprojectnamespace.PilotInterface
export class Pilot implements Pilotinterface {
    private vec: Vehicle;
    public _namepilot: string;
    private _agepilot: number;

    constructor(name, age,vec: Vehicle) {
        this.vec = vec;
        this._agepilot = age;
        this._namepilot = name;
    }

    get namepilot(): string {
        return this._namepilot;
    }

    set namepilot(value: string) {
        this._namepilot = value;
    }

    get agepilot(): number {
        return this._agepilot;
    }

    set agepilot(value: number) {
        this._agepilot = value;
    }

    driving() {
        return this.vec.driving();
    }
}
}

这是我的班级 Car.ts ,这个班级继承自另一个班级车辆

namespace tsprojectnamespace {
import TypeVehicleInterface = tsprojectnamespace.TypeVehicleInterface;
import PilotInterface = tsprojectnamespace.PilotInterface;
export class Car extends Vehicle implements TypeVehicleInterface, PilotInterface {
    private _pilot : Pilot;
    private _brand: string;
    private _model: string;
    private _torque: number;
    constructor(brand, model, torque, weight, speed, pow) {
        super(weight, speed, pow);
        this._brand = brand;
        this._model = model;
        this._torque = torque;
    }

    get pilot(): tsprojectnamespace.Pilot {
        return this._pilot;
    }

    set pilot(value: tsprojectnamespace.Pilot) {
        this._pilot = value;
    }

    get brand(): string {
        return this._brand;
    }

    set brand(value: string) {
        this._brand = value;
    }

    get model(): string {
        return this._model;
    }

    set model(value: string) {
        this._model = value;
    }

    get torque(): number {
        return this._torque;
    }

    set torque(value: number) {
        this._torque = value;
    }

    drive() {
        return `the car's model is ${this.brand} ${this.model}`;
    }

    driving() {
        return `the pilot ${this.pilot.namepilot} is driving the Car ${this.brand} ${this.model}`;
    }
}
}

我的意思是,Car.ts返回一个包含汽车基本信息的函数,这是控制台的输出而没有获得名称导航

the pilot is driving the Car Lamborghini egoista
the pilot is driving the Car Ferrari sergio
the pilot is driving the ship
the pilot is driving the ship
The pilot is planning the Plane
The pilot is planning the Plane

Process finished with exit code 0

这就是我想要的

the pilot 'lewis' is driving the Car Lamborghini egoista
the pilot 'ferdinand'is driving the Car Ferrari sergio
the pilot "" is driving the ship
the pilot "" is driving the ship
The pilot "" is planning the Plane
The pilot "" is planning the Plane

这就是我得到的

        return "the pilot " + this.pilot.namepilot + " is driving the Car " + this.brand + " " + this.model;
                                        ^

TypeError: Cannot read property 'namepilot' of undefined
at Car.driving (C:\Users\Downloads\ts project\final.js:163:45)
at Pilot.driving (C:\Users\Downloads\ts project\final.js:78:29)
at Function.doActions.pushElements (C:\Users\Downloads\ts project\final.js:298:32)
at tsprojectnamespace (C:\Users\Downloads\ts project\final.js:331:15)
at Object.<anonymous> (C:\Users\Downloads\ts project\final.js:333:3)
at Module._compile (module.js:556:32)
at Object.Module._extensions..js (module.js:565:10)
at Module.load (module.js:473:32)
at tryModuleLoad (module.js:432:12)
at Function.Module._load (module.js:424:3)

Process finished with exit code 1

我该怎么办?

更新

我已经增加了2个类,如船舶和飞机一样延伸车辆

我忘记了我的测试变量

        let pilot1 = new Pilot(`lewis`, 50, new Car(`Lamborghini`, `egoista`, 1200, 1300, 360, 650));
        let pilot2 = new Pilot(`ferdinand`, 34, new Car(`Ferrari`, `sergio`, 900, 1200, 320, 750));

        pilots.push(pilot2, pilot1);
        console.log(pilot1.driving());
        console.log(pilot2.driving());

2 个答案:

答案 0 :(得分:0)

您确定先将您的飞行员归功于您的车吗?

myCar.pilot = pilotHamilton;

您省略了测试代码的一部分,因此我们真的不知道您的实例化Car类的pilot变量是否设置在测试类的某个位置。

答案 1 :(得分:0)

我认为问题在于Car对象永远不会被告知谁在驾驶它。

您可以更新Pilot构造函数:

 constructor(name, age,vec: Vehicle) {
     this.vec = vec;
     vec.pilot = this;
     this._agepilot = age;
     this._namepilot = name;
 }

这样你的Pilot对象就会将自己分配给它在实例化时驱动的汽车。

修改

或者,如果不能选择更改类的实现,则可以更改实例化对象的方式:

// Instantiate the cars 
let lamborghini = new Car(`Lamborghini`, `egoista`, 1200, 1300, 360, 650);
let ferrari = new Car(`Ferrari`, `sergio`, 900, 1200, 320, 750);

// Instantiate the pilots
let pilot1 = new Pilot(`lewis`, 50, lamborghini);
let pilot2 = new Pilot(`ferdinand`, 34, ferrari);

// Link the pilots to their respective car
lamborghini.pilot = pilot1;
ferrari.pilot = pilot2;

作为旁注,可能值得考虑重写Pilot类或Car类。第二种解决方案取决于我们对Pilot和Car类如何实施的了解,这是不理想的。

至少,如果有可能在阻止其正常执行的条件下调用Car.driving()方法,那么应该进行一些验证。