es6之前 JavaScript程序使用函数和基于原型的继承来创建可重用的组件 es6之后 JavaScript程序员将能够使用基于类的面向对象的方式。
/eg01 类的创建
class Greeter {
greeting:string
constructor(message:string){
this.greeting = message
}
greet(){
return 'hello,' + this.greeting
}
}
let greeter = new Greeter("world")
class Animal {
move(a:number){
console.log(a)
}
}
class Dog extends Animal{
bark(){
console.log('woof')
}
}
const dog = new Dog()
dog.bark()
dog.move(10)
dog.bark()
class Animals {
name: string
constructor(tName:string){
this.name = tName
}
move(a:number){
console.log('move:>> ', a);
}
}
class Snake extends Animals {
constructor(name:string){
super(name)
}
move(a=5){
console.log('snake-move :>> ', a);
super.move(a)
}
}
class Horse extends Animals {
constructor(name:string){
super(name)
}
move(a=45){
console.log('Horse-move :>> ', a);
super.move(a)
}
}
let sam = new Snake('sammy')
let tom = new Horse('Tommy')
sam.move()
tom.move(34)
|