//继承
class Person {
name:string = ''
say(){
console.log('我是'+this.name)
}
}
class Student extends Person{
say(){
super.say()
console.log('我是学生'+this.name)
}
}
//Student 会继承所有的Person属性和方法
let a = new Student()
a.say() //会覆盖Person方法
//调用say方法会覆盖Person的方法,如果不想覆盖Person方法 就需要调用一下他的say方法使用super方法
//抽象类 abstract 本身不能实例化,可以被继承
abstract class Person {
name: string = ''
abstract say();
}
class Student extends Person {
say() {
//必须实现抽象类的方法
}
}
// 接口
//人 狼 和人狼
class Person {
name:string =''
}
interface IWolf{
sttack();
}
interface Dog{
eat();
}
// 多个特性
class WolfMan extends Person implements IWolf,Dog{
// 必须实现接口方法 必须实现
sttack(){
}
eat(){
}
}
//属性寄存器
class Person {
_hp = 100
// 取值
get hp() {
return this._hp
}
// 赋值
set hp(value) {
if (value < 0) {
this._hp = 0
} else {
this._hp = value
}
}
}
let a = new Person()
a.hp = -180
console.log(a.hp, 'hp');