为什么字段声明必须在 class 中,因为它实现了一个接口
Why field declaration is must in class as it implements an interface
我想澄清我在 class 上实现接口的概念。
An interface is something like a template which cannot make an impact until a class implements it. (link)
所以如果我将接口定义为:
interface IVehicle {
color: string,
model_no: number,
}
然后我将 class 设为:
class Vehicle implements IVehicle {
}
它在 class 名称处给了我红色下划线。为什么我必须在 class 中再次声明字段,因为它正在实现一个不能获取其字段的接口?
为什么一定要这样写?
class Vehicle implements IVehicle {
color: string;
model_no: number;
}
那aclass不能fetch其实现的接口的字段是什么接口,如果我不实现接口直接在a[=中声明字段怎么办? 30=]。我在想为什么 TypeScript 的开发者要添加这个东西?它加倍了代码;首先制作一个接口,在那里声明字段,然后制作一个 class (还添加 implements InterfaceName
),然后再次在 class 中声明这些字段,为什么?
因为这也是有效的:
interface IVehicle {
color: string;
model_no: number;
}
class ClownCar implements IVehicle {
// can be a subset of original type
public color: 'red' | 'green' | 'blue';
public model_no: 250 = 250;
}
class NonEditableCar implements IVehicle {
// can use getter or setters instead, doesn't have to be normal field
get color() {
return 'grey';
}
get model_no(){
return 100;
}
}
一个接口只是说一个实例将有那些字段,并没有说它必须匹配那个确切的类型。 class 中的声明指定实现是存储需要初始化的实例变量。
您可以在仍然实现接口的同时缩小实例变量并扩大方法调用签名:
interface VehicleCarrier {
contents: IVehicle[];
launch(count: number): void;
}
class AircraftCarrier implements VehicleCarrier {
// this is more specific, and because the inteface forces you to write it you have to consider that
// it should be more specific.
public contents: (Plane | Submarine)[];
// can extend call signatures for methods
public launch(count: number, type: 'plane' | 'sub' = 'plane') {}
}
我想澄清我在 class 上实现接口的概念。
An interface is something like a template which cannot make an impact until a class implements it. (link)
所以如果我将接口定义为:
interface IVehicle {
color: string,
model_no: number,
}
然后我将 class 设为:
class Vehicle implements IVehicle {
}
它在 class 名称处给了我红色下划线。为什么我必须在 class 中再次声明字段,因为它正在实现一个不能获取其字段的接口?
为什么一定要这样写?
class Vehicle implements IVehicle {
color: string;
model_no: number;
}
那aclass不能fetch其实现的接口的字段是什么接口,如果我不实现接口直接在a[=中声明字段怎么办? 30=]。我在想为什么 TypeScript 的开发者要添加这个东西?它加倍了代码;首先制作一个接口,在那里声明字段,然后制作一个 class (还添加 implements InterfaceName
),然后再次在 class 中声明这些字段,为什么?
因为这也是有效的:
interface IVehicle {
color: string;
model_no: number;
}
class ClownCar implements IVehicle {
// can be a subset of original type
public color: 'red' | 'green' | 'blue';
public model_no: 250 = 250;
}
class NonEditableCar implements IVehicle {
// can use getter or setters instead, doesn't have to be normal field
get color() {
return 'grey';
}
get model_no(){
return 100;
}
}
一个接口只是说一个实例将有那些字段,并没有说它必须匹配那个确切的类型。 class 中的声明指定实现是存储需要初始化的实例变量。
您可以在仍然实现接口的同时缩小实例变量并扩大方法调用签名:
interface VehicleCarrier {
contents: IVehicle[];
launch(count: number): void;
}
class AircraftCarrier implements VehicleCarrier {
// this is more specific, and because the inteface forces you to write it you have to consider that
// it should be more specific.
public contents: (Plane | Submarine)[];
// can extend call signatures for methods
public launch(count: number, type: 'plane' | 'sub' = 'plane') {}
}