class Animal{
    public name;
    public constructor(name){
        this.name = name;
    }
}

        let a = new Animal('lx');
        cc.log(a.name);     //如果用private修饰就不能访问
        a.name = 'hello';
        cc.log(a.name)
class Animal{
    protected name;
    public constructor(name){
        this.name = name;
    }
}

class Cat extends Animal{
    constructor(name){
        super(name);
        this.name = 'xx'; //private修饰的子类也不能访问,protected修饰的可以
    }
}

当构造函数修饰为 private 时,该类不允许被继承或者实例化

当构造函数修饰为 protected 时,该类只允许被继承

class Animal{
    public readonly name;
    public constructor(name){
        this.name = name;
    }
}
  let temAnimal = new Animal('aaa');
  temAnimal.name = 'ddd'; //只读属性不能赋值
//抽象类是不允许被实例化
//抽象类中的抽象方法必须被子类实现
abstract class Animal {
    public name;
    public constructor(name) {
        this.name = name;
    }
    public abstract sayHello();
}

class Cat extends Animal {
    constructor(name) {
        super(name);
    }
    public sayHello()
    {

    }
}

let a: Cat = new Cat('msk');    //类的类型

 

posted @ 2020-12-13 15:41  小翔momo  阅读(48)  评论(0编辑  收藏  举报