接口
接口定义了一个规范
typescript
// 描述一个对象的类型
type myType = {
name:string,
age:number
}
// 接口用来定义一个类的结构,用来定一个类中应该包含哪些属性和方法
// 同时接口也可以当成类型声明去使用
// 接口可以多次赋值
interface myInterface{
name:string,
age:number
}
// 接口中所有的属性都不能有实际的值
// 接口只定义对象的几个,而不考虑实际的值
interface myInterface{
gender:string,
sayHello():void;
}
const obj:myInterface = {
name:'sss',
age:20,
gender:'男',
sayHello() {
console.log("哈哈哈")
}
}
console.log(obj);
interface myInter{
name:string
sayHello():void;
}
// 定义类时,可以使类去实现一个接口
// 实现接口就是使类满足接口的要求
class MyClass implements myInter{
name:string;
constructor(name) {
this.name = name
}
sayHello() {
console.log('aaa')
}
}