好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

TypeScript(类—继承—多态)

一、类的定义(class)

 

  类(class)这个概念来源于OOP(Object Oriented Programming),也就是面向对象编程,OOP是一种计算机编程架构,其有着封装,继承,多态三种特性。而类在OOP中是实现信息封装的基础。类是一种用户定义类型,也称类类型。每个类包含数据说明和一组操作数据或传递消息的函数。类的实例称为对象。

 

  es5:

          function   Person(name) {
              this .name =  name;
              this .run =  function   () {
                console.log(  this  .name)
            }
        }
        let p  =  new  Person('李四' )
        p.run()  // 李四  

  ts:

  class Person{       
    name:string;      /*   属性,前面省略public关键词     */  
    constructor(name:string){     /*   构造函数,实例化类的时候触发的方法   */ 
         this .name= name
    }
    get():  void  {
        console.log(  this  .name)
    }
    set(name:string):  void  {
          this .name= name
        console.log(  this  .name)
    }
}
let p = new  Person('李四' )
p.get()       /*   李四   */  
p.set( '张三')      /*   张三   */  
p.get()       /*   张三   */  

 

二、继承(extends、super)

  class Futher {
    name: string;
    constructor(name: string) {
          this .name =  name
    }
    run():   void   {
        console.log(  this  .name)
    }
}
let p1  =  new  Futher('张三' )
p1.run()   //   张三 
 class Child extends Futher {
    constructor(name: string) {
        super(name)   /*   初始化父类的函数   */  
    }
    work():   void   {
        console.log(  this  .name)
    }
}
let p2  =  new  Child('李四' )
p2.run()   //   李四,子类继承父类的方法,若子类存在该方法则优先使用子类的方法 
p2.work()    //   李四,子类可以拓展自己的方法  

 

三、类里的修饰符(public、protected、private)

  public:公有,不加修饰符默认公有。类里、子类、外部可以访问

  protected:保护类型,类里、子类可以访问,类外不可访问

  private:类里可以访问,子类、外部不可访问

  class Futher {
    public name: string;      /*   公有   */  
    protected age: number  |  null  | undefined;     /*   保护   */  
    private phone: number  |  null  | undefined;     /*   私有   */  
    constructor(name: string, age ?: number, phone? : number) {
          this .name =  name
          this .age =  age
          this .phone =  phone
    }
    run():   void   {
        console.log(  this .name,  this .age,  this  .phone)
    }
}
class Child extends Futher {
    constructor(name: string, age: number, phone ? : number) {
        super(name, age, phone)   /*   初始化父类的函数   */  
    }
    work():   void   {
        console.log(  this .name,  this  .age)
          /*   console.log(this.name,this.age,this.phone)   */    /*   子类添加private报错   */  
    }
}
let p1  =  new  Futher('李四', 18, 123456 )
let p2  =  new  Child('张三', 20, 654321 )
p1.run()   /*   李四 18 123456,类里可以访问public\protected\private   */  
p2.work()  /*    张三 20,子类可以访问public\protected,子类访问private报错   */  
console.log(p1.name)   /*    张三,李四,外部可以访问public   */ 
 /*   console.log(p1.name,p1.age,p1.age)  */   /*   error,外部访问protected\private报错   */  

 

四、静态方法(static)

  class Person {
    public name: string;      /*   公有   */  
    static sex:string ='男';  /*   静态属性   */  
    constructor(name: string) {
          this .name =  name
    }
    run():   void  {    /*   实例方法,实例后调用   */  
        console.log(  this  .name)
    }
    static work():  void {  /*   静态方法,不能直接调用类里的属性   */ 
         /*   console.log(this.name)   */    /*   error,不能直接调用类里的属性   */  
        console.log(Person.sex)   /*   男   */  
    }
}  

 

五、多态

  多态:父类定义一个方法不去实现,让继承他的子类去实现,每一个子类有不同的表现

  多态属于继承

  class Futher {
    public age: number;
    constructor(age: number) {
          this .age =  age
    }
    counts():   void   {
        console.log(  this  .age)
    }
}
class children1 extends Futher {
    constructor(age: number) {
        super(age)
    }
    counts():   void  {     /*   多态,重写方法不执行父类方法   */  
        console.log(  this .age - 1 )
    }
}
class children2 extends Futher {
    constructor(age: number) {
        super(age)
    }
    counts():   void   {
        console.log(  this .age + 1 )
    }
}  

 

六、抽象类(abstract)

  用abstract定义抽象类和抽象方法,抽象类中的抽象方法不包含具体实现并且必须在派生类中实现

  1、抽象方法必须在抽象类中

  2、抽象类和抽象方法是一个标准,定义标准后,子类中必须包含抽象定义的方法

 abstract class Futher {  /*   定义一个抽象方法   */  
    public age: number;
    constructor(age: number) {
          this .age =  age
    }
    abstract counts(): any;   /*   抽象方法必须在抽象类中   */  
}
class children extends Futher {
    constructor(age: number) {
        super(age)
    }
    counts():   void  {     /*   子类中必须有抽象类中的抽象方法   */  
        console.log(  this .age - 1 )
    }
}  

 

查看更多关于TypeScript(类—继承—多态)的详细内容...

  阅读:67次