接口

发布时间 2023-09-03 18:30:12作者: linux星

接口(Interface)

接口提供了一种约定,用于确保对象的属性和方法遵循特定的模式。接口只能包含抽象的声明,不能包含具体的实现。接口本身不能被实例化,它可以被类所采用,以提供具体的实现。

在 ts 中,经常把json数据转为 interface 或 type 。但在 uts 中,只适合转 type,不适合使用 interface。因为 interface 在 kotlin 和 swift 中另有不同。

接口使用关键字 interface 声明。

interface IPerson {
  name: string;
  printName(): void;
}
复制代码

#接口继承

接口可以继承一个或多个接口:

class IPerson1 extends IPerson {}
复制代码

#实现接口

类可以实现一个或多个接口: