zoukankan      html  css  js  c++  java
  • Typescript学习总结之类

    1. 类的定义和使用

    class Student {
        name;
    
        say() { 
            console.log(this.name + " saying");
        }
    }
    
    var s1 = new Student();
    s1.name = "zhangsan";
    s1.say();
    
    var s2 = new Student();
    s2.name = "lisi";
    s2.say();
    

      

    2. 类的构造函数

    class Student {
    
        constructor(name: string) { 
            this.name = name;
        }
    
        name;
    
        say() { 
            console.log(this.name + " saying");
        }
    }
    
    var s1 = new Student("zhangsan");
    s1.say();
    
    var s2 = new Student("lisi");
    s2.say();
    

     类的构造函数定义在constructor

    效果图

    上面的代码可以简写为

    class Student {
    
        constructor(public name: string) { 
        }
    
        say() { 
            console.log(this.name + " saying");
        }
    }
    
    var s1 = new Student("zhangsan");
    s1.say();
    
    var s2 = new Student("lisi");
    s2.say();
    

      输出结果是一样的

    3. 类的继承

    class Student {
    
        constructor(public name: string) { 
        }
    
        say() { 
            console.log(this.name + " saying");
        }
    }
    
    class HighSchoolStudent extends Student { 
    
        constructor(name: string, no: string) {
            super(name)
         }
        no:string;
        study() {
            super.say();
         }
    }
    
    var hStudent = new HighSchoolStudent("wangwu","06169020");
    hStudent.say();
    

      

    extends 代表要继承的类

    super(name)调用父类的构造函数

    super.say(); 调用父类的方法

  • 相关阅读:
    codevs-1205
    codevs-1204
    C++STL 求和:accumulate 【转】
    map映照容器
    set集合容器
    HDOJ-1263
    HDOJ-1004(map)
    紫书 例题 10-12 UVa 1637(概率计算)
    紫书 例题 10-11 UVa 11181(概率计算)
    紫书 例题 10-10 UVa 10491(概率计算)
  • 原文地址:https://www.cnblogs.com/linlf03/p/8448940.html
Copyright © 2011-2022 走看看