我对这些课程有疑问。我想使用doSomething()
class 独有的方法B
而不每次都进行类型转换,但是当我将 property 指定a
为 typeB
时,它告诉我它没有在构造函数中赋值,这在父构造函数中进行赋值时有些错误。
class A {
}
class B extends A {
doSomething() { }
}
class One {
constructor(protected a: A){ }
}
class Two extends One {
protected a: B // Property 'a' has no initializer and is not definitely assigned in the constructor.
constructor(){
super(new B());
// If I enter "this.a = new B();" here then the error disappears, but the code is redundant.
}
doStuff() {
this.a.doSomething()
}
}
我究竟做错了什么?