ts中接口的使用

ts中接口的使用

(function(){
    // 描述一个对象的类型
    type myType ={
        name:string,
        age:number
    }


    // 类也是用来创建对象的
    /* 接口就是用来定义一个类(对象) 的结构*/
    interface myInterfance {
        name:string,
        age:number
    }

    const obj:myInterfance = {
        name:'zhansan',
        age:18
    }

    /* 
    接口可以在定义类的时候去限制类的结构
        接口中所有的属性都不能有实际的值
        接口只定义对象的结构,而不考虑实际值
        在接口中所有的方法都是抽象方法
    */
    interface myInter {
        name:string;

        sayHello():void
    }

    class MyClass implements myInter {
        name: string;

        constructor(name:string){
            this.name= name
        }

        sayHello(): void {
           console.log('大家好');
        }
    }

})()
原文地址:https://www.cnblogs.com/malong1992/p/15376131.html