TypeScript笔记七

/**
 * Created by ufo9631 on 2017/5/25.
 */
//泛型:参数化的类型,一般用来限制集合的内容
/**
 * 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 worker:Array<Person>=[];//数组只能放Persong类型
worker[0]=new Person("张三");
worker[1]=new Employee("李四","2");
//worker[2]=2;这样是错误的 只能放Person类型的数据
//实例化类
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";

  

posted @ 2017-11-15 22:52  编程猴子  阅读(97)  评论(0编辑  收藏  举报