7、Typescript 数组接口、类接口

案例

// 1、数组接口
interface ArrayInterface {
    // 索引index是必须的
    [index:number]:string;
}
let arrayInfo:ArrayInterface = ['a', 'b', 'c'];
console.log(arrayInfo);

// 2、定义接口
interface ClassInterface {
    // 定义方法
    showInfo(str:string):any
}

class ClasOne implements ClassInterface {
    // 实现接口方法
    showInfo(str:string){
        console.log('ClasOne====' + str);
    }
}
class ClassTwo implements ClassInterface {
    // 实现接口方法
    showInfo(str:string){
        console.log('ClassTwo====' + str);
    }
}

let c1 = new ClasOne();
c1.showInfo('zs');
let c2 = new ClassTwo();
c2.showInfo('lis');
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容