类的定义
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| class P { name: string age: number constructor(naem: string, age: number) { this.name = name this.age = age }
play() { console.log(this.name + '在打来福') } }
const p = new P('常威', 18) p.play()
|
类的继承
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
| class P { name: string age: number constructor(naem: string, age: number) { this.name = name this.age = age } eating() { console.log('在吃饭') } }
class CW extends P { idCard: number constructor(name: string, age: number, idCard: number) { super(name, age); this.idCard = idCard } eating() { super.eating() console.log('常威在吃山珍海味') } play() { console.log(this.name + '在打来福') } }
class LF extends P { sno: number constructor(name: string, age: number, sno: number) { super(name, age); this.sno = sno } eating() { console.log('来福在吃饭') } beaten() { console.log(this.name + '被常威打') } }
const cw = new CW('常威', 18, 001) cw.eating() cw.play() const lf = new LF('来福', 18, 9527) lf.eating() lf.beaten()
|
类的多态
父类引用(类型)指向子类对象; 多态的目的是为了写出更具通用性的代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| class P { action() { console.log('有行动') } }
class CW extends P { action() { console.log('常威在打来福') } }
class LF extends P { action() { console.log('来福在被常威打') } }
function makeActions(p: P[]) { p.forEach(item => { item.action() }) }
makeActions([new CW(), new LF()])
|
类的成员修饰符
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| class P { public name: string = '常威' private age: number = 18 getAge() { return this.age } setAge(newAge) { this.age = newAge } }
|
只读属性
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| class P { readonly name: string age?: number readonly friend?: P constructor(name: string, friend?: P) { this.name = name this.friend = friend } }
const p = new P('常威', new P('来福')) console.log(p.name) console.log(p.friend)
if (p.friend) { p.friend.age = 18 }
|
getters和setters
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| class P { private _naem: string constructor(name: string) { this._name = name } set name(newName) { this._name = newName } get name() { return this._name } }
const p = new P('常威') p.name = '来福' console.log(p.name)
|
类的静态成员
1 2 3 4 5 6 7 8 9 10
| class P { static time: string = '12:00' static eating() { console.log('到点了, 干饭了') } }
console.log(P.time) P.eating()
|
抽象类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
| abstract class Shape { abstract getArea() }
class Rectangle extends Shape { private width: number private height: number constructor(width: number, height: number) { super(); this.width = width this.height = height } getArea() { return this.width * this.height } }
class Circle extends Shape { private r: number constructor(r:number) { super(); this.r = r } getArea() { return this.r * this.r * 3.14 } }
function makeArea(shape: Shape) { return shape.getArea() }
const rectangle = new Rectangle(20, 30) const circle = new Circle(10)
console.log(makeArea(rectangle)) console.log(makeArea(circle))
|
类的类型
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| class P { name: string = '常威' eating() { console.log('常威在吃饭') } }
const p: P = { name: '常威', eating() { console.log('常威在吃好吃的') } }
function fn (p: P) { console.log(p.name) }
fn(new P) fn({name: '常威', eating() {console.log('常威在吃山珍海味')}})
|