1, class definition
// TS define classes and ES6 similar, except that the modifier attributes need to define the data type and class the Person { public name: String ; constructor (n-: String ) { the this .name = n-; } RUN (): void { Console. log ( the this .name); } } var P = new new the Person ( ' John Doe ' ); p.run ()
2, inside the class modifiers
public : the public , which in the current class, subclass, class outside can access protected : protection type , in which the current class, subclass which can be accessed from outside the class can not access Private : Private , which can be accessed in the current class, subclass, the class had not even access external property if not default modifier is public (public )
3, class inheritance
// inherited and ES6 ts similar class, extends, and Super class {the Person name: String; constructor (name: String) { the this .name = name; } RUN (): String { return `$ { the this .name motion} ` } } var P = new new the Person ( 'John Doe' ); Alert (p.run ()) class the extends the Web the Person { constructor (name: String) { Super (name); / * initialize the parent class's constructor * / } } var W = new new the Web ( 'John Doe' ); Alert (w.run ());
4, the multi-state (or inherited)
// Polymorphic: parent class defines a method not implemented, so that its subclasses inherit each subclass to achieve different performance class Animal { name: String; constructor (name: String) { the this .name = name; } // What specific inherited eat to achieve its subclasses, each subclass is not the same performance? eAT () { console.log ( 'method of eating' ) } } class Dog the extends Animal { constructor (name: String) { Super (name) } eAT () { return the this .name + 'eat dog food' } } class Cat {Animal the extends constructor (name: String) { Super (name) } eAT () { return the this .name + 'fish' } }
5, static properties and methods
// Properties foregoing methods add and static, static properties and methods that class the Person { public name: String; static Age = 20 is // static properties constructor (name: String) { the this .name = name; } // example of a method run ( ) { console.log ($ { ` the this .name} in motion ') } // static method static Work () { console.log (' I am the age of` $ {person.age}) } } // call instantiated method var P = new new the Person ( 'John Doe' ) p.run (); // call the static method and attributes Person.work (); person.Age;
6, abstract methods
// class and method before adding keywords abstract, and abstract method of the abstract class // abstract method of the abstract class can be placed inside the abstract class Animal { public name: String; constructor (name: String) { the this .name = name; } // abstract method does not contain the specific implementation, and must be implemented in the derived class abstract EAT (): void ; } class the extends Animal Dog { constructor (name: String) { Super (name) } // subclasses of the abstract class must abstract methods inside the abstract class eAT () { the console.log ( the this .name + 'eat food' ) } } var D = new new Dog ( 'dog' ); d.eat ();