简单Demo
enum Msg {
HI = 'Hi',
HELLO = 'Hello'
}
interface A {
say(msg: Msg): void
}
class B implements A {
say(msg: Msg): void {
console.log(msg + ',I am B')
}
}
let a: A = new B()
a.say(Msg.HI)
复杂Demo
class Rectangle {
private width: number
private length: number
constructor(width: number, length: number) {
this.width = width
this.length = length
}
public area(): number {
return this.width * this.length
}
}
class square extends Rectangle {
constructor(side: number) {
super(side, side)
}
}
let s = new square(10)
console.log('正方形的面积:', s.area())
参考文档
黑马bilibili