TypeScript类的接口

阅读 62

2022-02-01

1.TypeScript类的接口

1.类 与 接口

// 接口
interface interHuman {
	speak():any;
}
// 类
class Human implements interHuman {
	speak() {
		console.log('我可以说话')
	}
}

这是最简单的用法,接口 interHuman对 Human 类的约束。

2.一个类可以使用两个或者多个接口进行约束。

// 接口 1
interface interHuman {
	speak():any;
}
// 接口2
interface inter2 {
 	walk():any;
}
// 类
class Human implements interHuman, implements inter2 {
	speak() {
		console.log('我可以说话')
	}
}

3.接口与接口之间可以实现继承。extends。

// 父接口 
interface interFather {
	father():any;
}
// 子接口
interface interSon extends interFather {
	son():any;
}
// 类
class Huamn implements interSon {
	son() {
		console.log('我是一个儿子')
	}
	father() {
		console.log('我是父亲')
	}
}
const d = new Huamn()
d.son()

精彩评论(0)

0 0 举报