8、TypeScript 接口继承接口,类实现多接口
2019-03-06 本文已影响2人
圆梦人生
1、ts类中只能继承一个父类
2、ts类中可以实现多少接口,使用(,)号分隔
3、ts接口中可以继承多个接口,使用(,)号分隔
案例:
interface InterfaceOne {
//
sports():void;
}
interface InterfaceTwo {
//
swimming():void
}
// 接口集成接口
interface InterfaceThree extends InterfaceOne,InterfaceTwo {
//
coding():void;
}
class ParentCls {
name:string;
constructor(name:string){
this.name = name
}
//
ktv(){
console.log(this.name + '唱歌');
}
}
// 继承父类实现多接口
class SubCls extends ParentCls implements InterfaceTwo, InterfaceOne {
// 实现接口
sports(){
console.log(this.name + '运动')
}
swimming(){
console.log(this.name + '游泳')
}
}
let subCls = new SubCls('小明');
subCls.sports()
subCls.swimming();
subCls.ktv()
//
class SubCls2 extends ParentCls implements InterfaceThree {
//
coding(){
console.log(this.name + '写代码');
}
sports(){
console.log(this.name + '运动');
}
swimming(){
console.log(this.name + '游泳')
}
}
let subCls2 = new SubCls2('小王');
subCls2.coding();
subCls2.sports();
subCls2.ktv();
subCls.swimming();