TYpeScript接口的使用

一、接口中的屬性值的使用:spa

 1 // 做用是強制類型檢查
 2 interface Iperson { 
 3     name: string;
 4     age: string;
 5 }
 6 
 7 class Person { 
 8     constructor(public config:Iperson) { 
 9 
10     }
11 }
12 
13 let p1: Person = new Person({
14     name: 'swe',
15     age:'12'
16 });

二、接口中的方法的使用code

沒有多態。使用效果與Java十分相似。blog

 1 interface Animal { 
 2     eat();
 3 }
 4 
 5 class Dog implements Animal { 
 6     eat() { 
 7         console.log('吃骨頭');
 8     }
 9 }
10 
11 class Cat implements Animal { 
12     eat() { 
13         console.log('吃魚');
14     }
15 }
16 let dog1 = new Dog();
17 dog1.eat();
18 let cat1 = new Cat();
19 cat1.eat();
相關文章
相關標籤/搜索