您的位置:首页 > 其它

1.2 类继承

2016-05-22 22:41 204 查看
extend实现类继承:

class Person{
name:string;
age:number;
constructor(name:string,age:number){
this.name=name;
this.age=age;
}
tell(){
return this.name+":"+this.age;
}

}

class Student extends Person{
school: string;
constructor(school:string){
this.school = school;
super("imi",80);
}
tell() {
return this.name + ":" + this.age+":"+this.school;
}

}

var s = new Student("逗币学员");

alert(s.tell())



编译后:

typescript帮我们省了这一步,

var __extends = (this && this.__extends) || function(d, b) {

    for (var p in b)

        if (b.hasOwnProperty(p)) d[p] = b[p];

    function __() { this.constructor = d; }

    d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());

};


var Person = (function() {

    function Person(name, age) {

        this.name = name;

        this.age = age;

    }

    Person.prototype.tell = function() {

        return this.name + ":" + this.age;

    };

    return Person;

}());

var Student = (function(_super) {

    __extends(Student, _super);

    function Student(school) {

        this.school = school;

        _super.call(this, "imi", 80); //初始化父级,如果这个没有传参的话,将会是“undefined”

    }

    Student.prototype.tell = function() {

        return this.name + ":" + this.age + ":" + this.school;

    };

    return Student;

}(Person));

var s = new Student("逗币学员");

alert(s.tell());
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: