ES6语法

    class Person{
        a(){
            alert(123)
        };
        b(){
            return 112233
        };
        constructor(aa=999){
            this.x=aa;
            this.bb=function(){
                alert(222)
            }
        };
    }
var p=new Person(1);
则x和bb是p的属性,而a和b在p的原型链上

TS语法

    class Person{
        x:number;
        bb;
        a(){
            alert(123)
        };
        b(){
            return 112233
        };
        constructor(public aa:number=999){
            this.x=aa;
            this.bb=function(){
                alert(222)
            }
        };
    }
var p=new Person(1);
此时constructor中的x和bb必须事先被声明,且默认为public
形参aa默认为private
为private的无法在外部被使用,仅有为public的才能作为实例p的属性被调用

TS中类的继承

class Me extends Person{
    p2p:any;
    constructor(b){
        super(b);
        this.p2p='666'
    };
    mover(){
        console.log('my mover','mover')
        // super.mover()
    }
}
var me:Person=new Me(66)

出现同名的属性和方法则以子类的为准,但是依然可以用 super.对应名的方式调用父类的内容
在子类的构造函数中用super()可以直接继承父类的构造函数

TS中公有,私有与受保护的修饰符

比较带有private或protected成员的类型的时候, 如果其中一个类型里包含一个private成员,那么只有当另外一个类型中也存在这样一个private成员, 并且它们是来自同一处声明时,我们才认为这两个类型是兼容的。 对于protected成员也使用这个规则。
例如:

class Animal {
    private name: string;
    constructor(theName: string) { this.name = theName; }
}
class Rhino extends Animal {
    constructor() { super("Rhino"); }
}
class Employee {
    private name: string;
    constructor(theName: string) { this.name = theName; }
}

let animal = new Animal("Goat");
let rhino = new Rhino();
let employee = new Employee("Bob");

animal = rhino;
animal = employee; // Error: Animal and Employee are not compatible

private的属性和方法只能在该class内部使用,被继承后在子类的constructor中虽然能用super()获取,但无法输出,也无法在方法中使用,因此可以认为private的属性只是为了给该class自身(不包括子)的方法提供参数,与其他都无关系;
而protected的属性和方法,虽然也不能在class外调用,但是一旦子class继承了该属性,可以在子class的方法中使用该值
同理,加了protected的构造函数,本身无法被实例化,但是继承其class的子类,可以实例化

静态属性 static

class Doit{
    static hello:number=12321;
    showHello(){
        console.log(Doit.hello)
    }
}
console.log(Doit.hello)
var doit=new Doit()
doit.showHello()

如上,加了static的属性,作为该类本身而非实例的属性被调用,在class内与class外都可以被调用