2017-10-04 43 views
0

有兩個接口,第一個是ICat,第二個是IMammalIMammal延伸ICat。請問IMammal中的Cat屬性是否有能力訪問ICat接口的所有屬性?我可以在打字稿中繼承一個接口到另一個接口嗎?我如何訪問繼承接口中定義的屬性?

export interface ICat { 
    Cateye: string[]; 
    Color: string;  
    Name: string; 
} 

export interface IMammal extends ICat { 
    Description: string; 
    HasImage: boolean; 
    CatGroup: string[]; 
    **Cat: ICat[]**; 
} 

基本上,我該如何在Typescript中實現多個接口繼承?

回答

0

我想是因爲貓是哺乳動物或許爲ICat應該擴展IMammal和哺乳動物並不需要爲ICat任何引用,如果你想添加的idog1天認爲:

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"]; 
0

呦uuse implements的接口,使用extends進行類繼承。 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);