1.2 类继承

来源:互联网 发布:无线怎么开启主人网络 编辑:程序博客网 时间:2024/05/16 10:29

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());


0 0
原创粉丝点击