vis*_*882 6 typescript angular
有两个接口,第一个是ICat,第二个是IMammal。
IMammal延伸ICat。中的Cat属性IMammal是否可以访问ICat接口的所有属性?
export interface ICat {
Cateye: string[];
Color: string;
Name: string;
}
export interface IMammal extends ICat {
Description: string;
HasImage: boolean;
CatGroup: string[];
**Cat: ICat[]**;
}
Run Code Online (Sandbox Code Playgroud)
基本上,我如何在Typescript中实现多个接口继承?
Gao*_*ter 12
我认为ICat应该扩展IMammal,因为Cat是哺乳动物,而哺乳动物不需要任何对ICat的引用,请考虑是否要有一天添加IDog:
export interface IMammal {
Description: string;
HasImage: boolean;
}
export interface ICat extends IMammal {
Cateye: string[];
CatGroup: string[];
Color: string;
Name: string;
}
class Cat implements ICat {
Cateye: string[];
Color: string;
Name: string;
Description: string;
HasImage: boolean;
CatGroup: string[];
}
const pusur:ICat = new Cat();
pusur.Name = "Pusur";
pusur.Description = "Likes lasagna";
pusur.CatGroup = ["Cartoon cat"];
Run Code Online (Sandbox Code Playgroud)
您用于implements接口,用于extends类继承。implements允许您传递由类实现的接口列表。
请注意,通常这并不重要,因为实现接口的所有属性和方法的类会自动与接口兼容,无论它是否显式implements接口,但显式列出接口至少意味着编译器会告诉如果您未能正确实施它们。
interface A {
a: string;
}
interface B {
b: string;
}
class Foo implements A,B {
a: string;
b: string;
}
function foo(b: B) {}
function bar(a: A) {}
const f = new Foo();
foo(f);
bar(f);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4082 次 |
| 最近记录: |