typescript-接口与class的区别

141 阅读1分钟

接口可以再定义类的时候去限制类的结构:1、接口中的所有属性不能有实际的值 2、接口只定义对象的结构,而不考虑实际值3、接口中所有的方法都是抽象方法

interface myInter{
    name:string,
    sayHello():viod
}

定义类的时候可以使类去实现一个接口实现接口就是使类满足接口的要求

class Myclass implements myInter{
    name:string;
    constructor(name:string){
        this.name=name
    }
    
    sayHello(){
        console.log("hello ts!")
    }
}