typescript---接口

1.接口一方面可以在面向对象编程中表示为行为的抽象,另外可以用来描述对象的形状;
2.接口就是把一些类中共有的属性和方法抽象出来,可以用来约束实现此接口的类
3.一个类可以继承另一个类并实现多个接口
4.接口像插件一样是用来增强类的,而抽象类是具体类的抽象概念
5.一个类可以实现多个接口,一个接口也可以被多个类实现,但一个类的可以有多个子类,但只能有一个父类

  1. 接口
  • interface中可以用分号或者逗号分割每一项,也可以什么都不加
    1.1 对象的形状
interface Speakable {
    speak(): void;
    name?: string;
    [propName: string]: any  //这句话的意思是,可以有其他的任何属性
}
let speakMan: Speakable = {
    speak():void {
    },
    name: "aaa",
    age: 18
 }

1.2 行为的抽象

//接口可以在面向对象编程中表示为行为的抽象
interface Speakable{
    speak():void;
}
interface Eatable{
    eat():void
}
//一个类可以实现多个接口
class Person implements Speakable,Eatable{  //可以实现多个接口
    speak(){
        console.log('Person说话');
    }
    eat(){}
}
class TangDuck implements Speakable{
    speak(){
        console.log('TangDuck说话');
    }
    eat(){}  //可以有自己的方法
}

1.3 任意属性

//无法预先知道有哪些新的属性的时候,可以使用 `[propName:string]:any`,propName名字是任意的
interface Person {
  readonly id: number;
  name: string;
  [propName: string]: any;
}

let p1 = {
  id:1,
  name:'zhufeng',
  age:10
}
p1.id = 5 //报错,因为id是只读的
  1. 接口的继承
  • 一个接口可以继承自另外一个接口
interface Speakable {
    speak(): void
}
interface SpeakChinese extends Speakable {
    speakChinese(): void
}
class Person implements SpeakChinese {
    speak() {
        console.log('Person')
    }
    speakChinese() {
        console.log('speakChinese')
    }
}
  1. readonly
  • 用 readonly 定义只读属性可以避免由于多人协作或者项目较为复杂等因素造成对象的值被重写
interface Person{
  readonly id:number;
  name:string
}
let tom:Person = {
  id :1,
  name:'zhufeng'
}
tom.id = 1;  // 报错
  1. 函数类型接口
  • 对方法传入的参数和返回值进行约束
interface discount{
  (price:number):number
}
let cost:discount = function(price:number):number{
   return price * .8;
}
  1. 可索引接口
  • 对数组和对象进行约束
  • userInterface 表示index的类型是 number,值的类型必须是 string
  • UserInterface2 表示:index 的类型是 string,值的类型也必须是 string
interface UserInterface {
  [index:number]:string
}
let arr:UserInterface = ['lc1','lc2'];
console.log(arr);

interface UserInterface2 {
  [index:string]:string
}
let obj:UserInterface2 = {name:'lc'};
  1. 类接口
  • 对类的约束
interface Speakable {
    name: string;
    speak(words: string): void
}
class Dog implements Speakable {
    name!: string;
    speak(words:string) {
        console.log(words);
    }
}
let dog = new Dog();
dog.speak('汪汪汪');
  1. 构造函数的类型
  • 在 TypeScript 中,我们可以用 interface 来描述类
  • 同时也可以使用interface里特殊的new()关键字来描述类的构造函数类型
class Animal{
  constructor(public name:string){
  }
}
interface WithNameClass{
  new(name:string):Animal
}
function createAnimal(clazz:WithNameClass,name:string){
   return new clazz(name);
}
let a = createAnimal(Animal,'lcc');
console.log(a.name);
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 接口 TypeScript 的核心原则之一是对值所具有的结构进行类型检查。它有时被称做“鸭式辨型法”或“结构性子类...
    罗彬727阅读 1,898评论 0 0
  • 介绍 TypeScript的核心原则之一是对值所具有的结构进行类型检查。 它有时被称做“鸭式辨型法”或“结构性子类...
    凌统丶阅读 3,332评论 0 1
  • 日期:2019 年 8 月 29 日 typescript 接口 介绍 TypeScript的核心原则之一是对值所...
    五十岚色叶阅读 3,056评论 0 2
  • 接口? 接口的作用在面向对象的编程中,接口是一种规范的定义,他定义了行为和动作的规范,在程序设计中,接口起到了一种...
    一号聪明阅读 2,701评论 0 0
  • TypeScript接口 接口只读属性 使用关键字readonly定义只读的接口属性 出现错误,如下 创建不可修改...
    小小小8021阅读 3,843评论 0 5