puddlejumper26 / blogs

Personal Tech Blogs
4 stars 1 forks source link

03 Class and Inheirtance in TypeScrit #24

Open puddlejumper26 opened 4 years ago

puddlejumper26 commented 4 years ago

1. Define Class in TS

```typescript class Person { name:string; //properties // constructor, methods trigged when instance created constructor(n:string) { this.name = n; } run ( ): void { console.log( this.name); } } ``` How to apply the Class ```typescript var p = new Person ( 'noob'); // here pass 'noob' to the property n inside constructor, then // initiate the constructor // pass the n to this.name, then // property inside run ( ) could be used. p.run(); ``` ```typescript class Person{ name:string; constructor(name:string){ this.name=name; } getName():string{ return this.name; } setName(name:string):void{ this.name=name; } } var p=new Person('noob'); alert(p.getName()); //noob p.setName('noob2'); alert(p.getName()); // noob2 ```

2. Inheritance in TS

**`extends`** **`super`** ```typescript class Person{ name:string; constructor(name:string){ this.name=name; } run( ):string{ return `${this.name} is running`; } } var p=new Person('noob'); p.run(); // noob is running; class Web extends Person{ constructor(name:string){ super(name); /* initiate constructor in parent*/ } } var w=new Web('noob2'); w.run( ); // noob2 is running ```

same method in child and parent

When there is same methos inside child and parent, normally first to use the method inside child. Only when the methos could not be found inside child, then go to the parent.

3. Static and Instance Method

```typescript class Person { public name:string = 'Shawn'; static age:number = 20; constructor(name:string) { this.name = name }; run ( ) { console.log(''runing"); // instance method // static method could not use public properties in class static print( ) { console.log('printing' + this.name) ; // wrong // static method could use static property static print2( ) { console.log('printing' + Person.age) ; // pass } // call the instance method const p = new Person( ); p.run ( ); // call the static method Person.print( ); ```

4. Polymophism 多态

**`polymophism`** : a method defined inside parent, without realization, and to be realized inside the children, each child has different realization. It is a type of `Inheritance`; ```typescript class Animal{ name:string; constructor( name:string){ this.name = name; } eat( ) { console.log('eating'); //The method for polymophism } class Dog extends Animal{ constructor(name:string){ super(name); } eat(){ return this.name + 'eating meat'; } class Cat extends Animal { constructor (name:string){ super (name); } eat() { return this.name+'eating fish'; } ```

5. Abstract Class

**`Abstract Class`** is the base class for others, can not be Instance directly,only through the extended classes. With **`abstract`** to define abstract class and method, `abstract method` could only be put inside `abstract class`; `Abstract Class` and `Abstract Method` are to provide a standard. `Abstract Method` **`must`** be realized inside `extended class`; ```typescript abstract class Animal { public name:string; constructor (name:string){ this.name = name;} abstract eat (): any; } const a = new Animal (); // WRONG, can not be Instance directly,only through the extended classes. class Dog extrends Animal { constructor (name:string){ super(name) } eat( ) { console.log(this.name+ 'eating meat') }; } const d = new Dog ( ); d.eat( ); ```