Class
In typescript, a structure similar to C #, where multiple classes can exist in a file, and the filenames can be arbitrarily taken, let's look at a simple example of a class.
1 class Person {2 private name:string;3 private age:number;4 5 Public Constructor (name:string, Age:number) {6 This. Name =name;7 This. Age =Age ;8 }9 Ten Public talk () { OneAlert ("Hi, my name is" + This. Name + ", I am" + This. age.tostring () + "!"); A } - } - the functionrun () { - varP:person =NewPerson ("Li Lei", 18); - P.talk (); - } + -Run ();
The classes in typescript need to be aware of the following points:
- Class without the access control, all classes are globally publicly accessible;
- If the properties and methods of the class are not added to the access control Fuzeme is considered public;
- 1.3 Start to support protected Access, only public and private before 1.3;
- Constructors are not class names but constructor;
- Access to the class's member variables and functions are required to add the this keyword;
Inheritance of Classes
In typescript, how to implement class inheritance, see the following example:
1 class Person2 {3 private name:string;4 private age:number;5 6 Constructor (name:string, Age:number)7 {8 This. Name =name;9 This. Age =Age ;Ten } One A Talk () - { -Alert ("Hi, my name is" + This. Name + ", I am" + This. age.tostring () + "!"); the } - } - - class Student extends person + { - private school:string; + A Constructor (name:string, Age:number, school:string) at { - Super (name, age); - This. School =School; - } - - Talk () in { - Super.talk (); toAlert ("and I ' m in" + This. School + "!"); + } - } the * functionRun () $ {Panax Notoginseng varP:person =NewStudent ("Li Lei", "中文版 School")); - P.talk (); the } + ARun ();
The inheritance of classes requires attention to the following points:
- Inheritance using extends keywords;
- Call the parent using the Super keyword;
- Overriding the parent method does not require the Override keyword to define the same method directly;
- A parent variable can be called to a child's method, conforming to the concept of polymorphism;
Getter/setter Package
In typescript we can encapsulate a member variable in getter and setter, as follows:
1 class Person2 {3 private _name:string;4 5 Constructor (name:string)6 {7 This. _name =name;8 }9 TenSet Name (value:string) { This. _name =value;} OneGet Name () {return This. _name;} A - Talk () - { theAlert ("Hi, my name is" + This. Name + "!"); - } - } - + functionRun () - { + varP:person =NewPerson (NULL); AP.name = "Li Lie"; at P.talk (); - } - -Run ();
static Data and methods
In typescript, static variables and static functions can be defined using static, and we take the Singleton class as an example to see:
1 class Person2 {3 private static _instance:person;4 5 static getinstance ():P Erson6 {7 if( This. _instance = =NULL)8 {9 This. _instance =NewPerson ();Ten } One return This. _instance; A } - - Talk () the { -Alert ("Hi, my name is Li lie!"); - } - } + - functionRun () + { A person.getinstance (). talk (); at } - -Run ();
Typescript Study Notes (c): Class