好得很程序员自学网

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

typescript_类

 //  类的定义 
 class Animal{
    id:string;  //  默认访问修饰符为 public : 类本身、子类、类外部可访问 
    public name:string;  //   public : 类本身、子类、类外部可访问 
    protected type:string = '动物';  //   保护成员 protected, 类本身、子类可访问, 类外部不可访问 
    private color:string;  //   私有成员 private: 类本身访问, 子类、类外部不可访问 

     //  构造函数 
     constructor(id:string, name:string,color:string){
          this .id =  id;
          this .name =  name;
          this .color =  color;
    }

      //  实例方法 
     run(){
        console.log(`${  this .name}(编号为:${ this  .id})在跑步`);
    }
}

  //  类的继承:继承关键字 extends 
 class Dog extends Animal{
    constructor(id:string, name:string, color: '白色' ){
        super(id, name,color);  //  调用父类的构造函数 
     }

    getDogInfo(){
          this .type = '小狗' ;
          //  console.log(`品种:${this.type}; 名字:${this.name}; 颜色:${this.color}`); // this.color 报错,原因是color在父类中是私有成员,只能在父类中访问,所以报错 
        console.log(`品种:${ this .type}; 名字:${ this  .name}; `);
    }
}

let xioahong  =  new  Dog('0001', '小白', '白色' );
xioahong.run();   //   输出    小白(编号为:0001)在跑步 
xioahong.getDogInfo();   //  输出    品种:小狗; 名字:小白; 

 //  类修饰符  
//  public : 公共属性,本类、子类、类外部均可访问  
//  protected: 保护成员,本类、子类中可访问,类外部不可访问  
//  private: 私有成员, 本类中可以访问,子类、类外部不可访问 

 //  类中的静态属性、静态方法 
 class Cat {
    name:string  = '小花猫' ;
    static tizhong:number  = 12 ;
    static eat(){
        console.log(`${  this  .name}的体重为${Cat.tizhong}斤`);
    }
}
Cat.tizhong  = 49;  //  调用静态属性 
Cat.eat();  //   调用静态方法, 输出:  Cat的体重为49斤  
/*  *
 * 解析:
 * 这里本来预期的输出应该是: 小花猫的体重为49斤, 但是 最终静态方法中无法获取到name属性的值
 * 静态方法是属于类的,即静态方法是随着类的加载而加载的,在加载类时,程序就会为静态方法分配内存
 * 非静态方法是属于对象的,对象是在类加载之后创建的
 * 静态方法先于对象存在,所以如果静态方法调用非静态方法的话,可能会报空指针异常。
 * 因此: 静态方法不能直接访问非静态属性,但是可以直接访问静态属性,访问方法为: [类名].[属性]
 * *  */ 

 //  抽象类: 

 /*  *
* 抽象的关键字: abstract * 抽象类是为其他类提供继承的基类,是给其他类定义的标准。 * 抽象类不能直接被实例化 * 抽象类必须包含至少一个抽象方法 * 抽象方法的声明不包含具体的实现 */ abstract class Person { abstract run(): void ; abstract getInfo():string; } // let p = new Person(); // 报错, 抽象类不能直接被实例化 /* * * 继承抽象类: * 在这里 Gril 为派生类, Person 为基类 * 派生类必须实现基类中的抽象方法 */ class Gril extends Person{ name:string; constructor(name:string){ super(); this .name = name; } // 这里实现了Person类中的抽象方法 run() run(){ console.log(`${ this .name}在跑步`) } // 这里实现了Person类中的抽象方法 getInfo() getInfo(): string { return this .name; } } let xiaohong = new Gril('小红' ); xiaohong.run(); // 输出:小红在跑步 console.log(xiaohong.getInfo()); // 输出 小红 // 多态 // 万物皆对象,任何对象都会有不同的形态,这叫多态,比如 树叶是一个对象,但是树叶有不同的形状,这就是多态,又比如,都是动物,但是动物又分很多种,这就是多态 // 在代码中,多态其实就是继承,提供一个基类给多个对象继承 /* * * 声明一个基类, 植物 */ class Botany { name:string; constructor(name:string){ this .name = name; } GrowUp(){ console.log(`我是${ this .name},我正在茁壮成长`); } } /* * * 仙人掌是一种植物,继承 植物 这个对象 */ class Cactus extends Botany{ thorn:string = '尖尖' ; constructor(name:string){ super(name) } growsThorns(): void { console.log(`我是一颗${ this .name},我身上会长很多${ this .thorn}的刺`); } } /* * * 百合花是一种植物,继承 植物 这个对象 */ class Lily extends Botany{ leaf:string = '绿色' ; constructor(name:string){ super(name) } growsLeaf(): void { console.log(`我是一颗${ this .name},我身上会长很多${ this .leaf}的叶子`); } } // 实例化一个仙人掌对象 let cactus = new Cactus('仙人掌' ); cactus.GrowUp(); // 输出: 我是仙人掌,我正在茁壮成长 cactus.growsThorns(); // 输出: 我是一颗仙人掌,我身上会长很多尖尖的刺 // 实例化一个玫瑰花的对象 let lily = new Lily('百合花' ); lily.GrowUp(); // 输出: 我是百合花,我正在茁壮成长 lily.growsLeaf(); // 输出: 我是一颗百合花,我身上会长很多绿色的叶子

 

查看更多关于typescript_类的详细内容...

  阅读:42次