• typeScript中 类的定义



    class Person{ public name:string; constructor(name:string){
    // 构造函数 实例化类的时候触发的方法 this.name = name; } getName():string{ return this.name; } setName(name:string):void{ this.name = name; } } let p = new Person('张三'); alert(p.getName()); p.setName('李四'); alert(p.getName());

    类的继承   extends    super

    class Children extends Person{
    
        constructor(name:string){
    
            super(name);  //super表示调用父类的构造函数
        }
    }
    
    var c = new Children('李四');
    alert(c.getName());

    类里的修饰符 (不加修饰符,默认为public)

    1. public:      公有 ,在类里,类外及子类都能访问
    2. protected:保护类型,在类里以及子类能访问
    3. private:    私有, 在类里才能被访问

    类的静态属性,静态方法  (可直接调用,不必实例化 )

    在class 类中 的方法或者属性前加 static 

    class Person{
        public name:string;
      static age:number = 20 constructor(name:string){
    // 构造函数 实例化类的时候触发的方法 this.name = name; } static run(){ //静态方法 静态方法里不能直接调用类里的属性 alert('在运动'+Person.age); } } Person.run();

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

    class Animal {
        public name:string;
        constructor(name:string) {
            this.name = name;
        }
    
        eat(){
            alert("吃的方法")
        }
    }
    
    class Dog extends Animal {
        constructor(name:string) {
            super(name);
        }
    
        eat(){
            return this.name + "吃骨头"
        }
    }
    
    class cat extends Animal {
        constructor(name:string) {
            super(name);
        }
    
        eat(){
            return this.name + '吃鱼头'
        }
    }

    抽象方法以及抽象类    

    抽象类:它是提供其它类继承的基类,不能直接被实例化

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

    抽象类和抽象方法用来定义标准

    注意:抽象方法只能放在抽象类中

    abstract class Animal {
        public name:string;
        constructor(name:string) {
            this.name = name;
        }
        //抽象方法在抽象类中不能实现
        abstract eat():void; 
    }
    
    class Dog extends Animal {
        constructor(name:string) {
            super(name);
        }
        //抽象类中的方法在子类中必须实现 
        eat(){
            return this.name + "吃骨头"
        }
    }
  • 相关阅读:
    差分约束
    关系运算图。。。(差分约束)
    克鲁斯卡尔算法+并查集
    整体代换(数学)
    魔性の分块 | | jzoj1243 | | 线段树の暴力
    论人品 | | noip1015模拟考
    hash 表 | | jzoj 1335 | | 脑残+手残 | | 集合的关系
    凸轮大总结
    Floyd | | jzoj[1218] | | [Usaco2009 Dec]Toll 过路费 | | BZOJ 1774 | | 我也不知道该怎么写
    topsort | | jzoj[1226] | | NOIP2003神经网络
  • 原文地址:https://www.cnblogs.com/webmc/p/12668538.html
Copyright © 2020-2023  润新知