接口是一种规约的约定,从接口继承的类必须实现接口的约定。在高级开发中,通常接口是用于实现各种设计模式的基础,没有接口,设计模式无从谈起。
定义接口:
interface ILog{
recordlog():boolean;
}
类从接口继承:
class Log4Net implements ILog{
public recordlog():boolean{
try{
console.log("log4net has been recored");
return true;
}
catch(e) {
console.log(e.message);
return false;
}
}
}
类的调用:
var log=new Log4Net();
log.recordlog();
另外接口也可以作为方法的参数类型,在具体实现时,就可以传入任何从接口继承的类的实现。
接口定义:
interface IStudent{
Name:string;
Age:number;
}
类的定义与方法:
class MiddleSchoolStudent{
constructor(public student:IStudent){
}
public GetMSSInfo(){
console.log(this.student.Name+" "+this.student.Age);
}
}
调用:
var mss=new MiddleSchoolStudent({
Name:"caojian",
Age:37
});
mss.GetMSSInfo();
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有