// extends
class AnimalTest {
public name: string;
protected love: string;
private age: number;
static aoao: string
constructor(theName: string,age:number,love:string) {
this.name = theName;
this.love = love;
this.age = age;
}
printPrivate():void{
console.log(this.name + "的年龄是:" + this.age);
}
printStatic():void{
console.log(this.name + "的static是:" + AnimalTest.aoao);
}
}
AnimalTest.aoao = "┗|`O′|┛ 嗷~~";
// static 通过类名访问 不能继承
// 个人感觉这个相当于const
var AnimalObj:AnimalTest = new AnimalTest('动物',12,'吃 睡 玩');
// public 可以 继承派生 默认是public 可以通过类的对象直接访问
console.log(AnimalObj.name);
// protected 可以 继承派生 不可以通过类的对象直接访问 以下会报错
// console.log(AnimalObj.age);
// private 不可以 继承派生 不可以通过类的对象直接访问 只能在类内部访问 但是定义类的方法可以继承 间接访问
class PersonTest extends AnimalTest{
printProtected():void{
console.log(this.name + "的爱好是:" + this.love);
}
}
var PersonObj:PersonTest = new PersonTest('一枝花',17,"学习 工作 精神享受");
console.log('--------------------')
console.log(PersonObj.name);
PersonObj.printPrivate();
PersonObj.printProtected();
PersonObj.printStatic();
class Viecle {
name:string;
protected constructor(name:string){
this.name = name;
}
}
// 构造函数定义为protected时 这个类不能直接new一个对象 但是她的子类可以new一个对象