TypeScript study notes (c)

class:

  1, TypeScript is object-oriented JavaScript, class describes the objects created common properties and methods

  2, the class keyword class declaration, use the extends keyword to inherit

  3, used when referring to a class member this keyword indicates that the member is a member of the class of our visit

  4, commonly referred to as the derived class subclasses , commonly referred to as the base class and superclass

  5, the derived class contains a constructor, it must call  super(), it will execute the constructor of the base class.

        In addition, access in the constructor  thisbefore the property, we certainly want to call  super(). This is an important rule TypeScript enforcement.

{Base class 
    name: String; 
    STR: String; 
    constructor (name: String) { 
        the this .name = name;
         // template string 
        the this .str = {$ `the Hello the this .name}, 
        Nice to Meet you ! `; 
    } 
    the sayHello () { 
        Alert ( the this .str); 
    } 
} 

class the extends One Base { 
    the sayHello () { 
        the this .str = the this .str + "\ nIn One" ; 
        super.sayHello ();     // use a method superclass 
    } 
} 

// pop Hello Lemon,
// nice to meet you!
// In one
new one("Lemon").sayHello();

 

Access modifiers:

  1, public: class members default to public, the public members can be accessed outside the class

  2, protected: the protection of members can only be accessed in the class, and its subclasses to

  3, Private: Private members can only be accessed in the class

supplement:

  1, TypeScript using a structural system, when comparing two different classes, if all members are compatible , then they are also compatible with the type of

  2, if the private or protected with the class members, only if the presence of another class corresponding to the type member, and they are from the same statement , which is compatible

 

Abstract class:

  1, as a base class to derived class to other use, it is generally not directly instantiated

  2, the implementation details of the abstract class can contain members, but must contain the keyword abstract , and may include access modifiers

Base class {abstract 
    constructor () { 
        Alert ( "the In Base" ); 
    } 
    public say () {} 
    public abstract RUN (); 
} 

class Test the extends Base { 
    // if not assigned to _str, the default is undefined 
    Private _str : String = "" ; 
    constructor () { 
        // subclasses need to call the constructor of the superclass in the constructor 
        Super (); 
        Alert ( "the in Test" ); 
    } 
    RUN () { 
        // need to use this keyword 
        this = ._str the this ._str + "Running \ n-" ; 
    } 
    say () { 
        the this ._str = the this._str + "Saying\n";
    }
    getStr() {
        alert(this._str);
    }
}

let t: test = new test(); // In base 然后 In test
t.run();
t.getStr();  // Running
t.say();
t.getStr();  // Running
             // Saying

 

Guess you like

Origin www.cnblogs.com/lemonyam/p/11229166.html