8、类和接口

 

  • 继承的困境
  • 类可以使用implements来实现接口

// 如果你希望在类中使用必须要被遵循的接口(类)或者别人定义的对象结构,可以使用implements关键字来确保其兼容

// 提取公共类型

interface Radio {

switchRadio(trigger: boolean): void:

}

calss Car implements Radio {

switchRadio(trigger: boolean) {

}

}

calss Cellphone implements Radio {

switchRadio(trigger: boolean) {

}

}

// 关联两种类型

interface Radio {

switchRadio(trigger: boolean): void:

}

interface Battery {

checkBatteryStatus(): void;

}

calss Cellphone implements Radio,Battery {

switchRadio(trigger: boolean) {

}

checkBatteryStatus() {

}

}

// 继承

// 新创建一个RadioWithBattery并继承Radio

interface RadioWithBattery extends Radio {

checkBatteryStatus(): void;

}

calss Car implements Radio {

switchRadio(trigger: boolean) {

}

}

calss Cellphone implements RadioWithBattery {

switchRadio(trigger: boolean) {

}

checkBatteryStatus() {

}

}

 

原文地址:https://www.cnblogs.com/shixiaokeng/p/14395818.html