接口
用来建立某种代码约定,使得其他开发者在调用某个方法或者创建新的类时必须遵守接口所定义的代码约定
1. 接口声明属性
interface IStudent {
name: string;
age: number;
}
class Student {
constructor(public iStudeng: IStudent) {
}
}
var s1 = new Student({name:"zhangsan",age: 30})
2. 接口声明方法
interface IStudent {
say();
}
class Student implements IStudent {
say() {
console.log("i am saying");
}
}
class HightStudent implements IStudent {
say() {
console.log("high school i am saying");
}
}
var s1 = new Student()
继承接口的类要实现接口中的方法,如上面的say方法