zoukankan      html  css  js  c++  java
  • TypeScript笔记六

    /**
     * Created by ufo9631 on 2017/5/25.
     */
    class  Person{
        public  name; //不指定访问控制符默认是public
        //类被实例化的时候会被调用
        constructor(name:string){
            //console.log("哈哈");
         this.name=name;
        }
    
    /*  可以把属性名简写在构造函数里
        constructor(public name:string){
            //console.log("哈哈");
            this.name=name;
        }
    */
        //访问控制符 public,private ,protected
        eat()
        {
            console.log(name);
        }
    }
    
    //继承使用关键字extends
    //用super关键字调用父类方法
    class  Employee extends Person
    {
        code:string;
    
        constructor (name:string,code:string)
        {
            super(name);//子类的构造函数必须调用父类的构造函数
             this.code=code;
        }
        work(){
       super.eat();
        }
    }
    
    //实例化类
    var p1=new Person("batman");
    //p1.name="";
    p1.eat();
    
    var p2=new Person("supermane");
    p2.name="supermane";
    p2.eat();
    
    var e1=new Employee("继承","123");
    e1.eat();
    e1.code="123";
    

      

  • 相关阅读:
    Python中的memoryview
    Python常见陷阱
    特殊方法 之 len __repr__ __str__
    collections模块
    使用math中的hypot实现向量
    Ellipsis对象
    array
    标准库heapq的使用
    Mysql常用命令
    使用npm查看安装的包
  • 原文地址:https://www.cnblogs.com/sumg/p/7841429.html
Copyright © 2011-2022 走看看