美文网首页我爱编程
TypeScript——类的继承

TypeScript——类的继承

作者: _____西班木有蛀牙 | 来源:发表于2018-05-23 00:32 被阅读23次
class Person {
    name:string;
    age: number;
    print () {
        return this.name + ':' + this.age;
    }
}

class Student extends Person {
    school:string;
    point() {
        return this.name + ':' + this.age + ':' + this.school;
    }
}

var S = new Student();
s.name = 'cc';
s.age = 19;
s.school = '清华';
s.print(); // cc:19:清华

或者

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

class Student extends Person {
    school:string;
    constructor (school: string) {
        super('cc', 19);
        this.school = school;
    }
    point() {
        return this.name + ':' + this.age + ':' + this.school;
    }
}

var S = new Student('清华');
s.print(); // cc:19:清华

相关文章

网友评论

    本文标题:TypeScript——类的继承

    本文链接:https://www.haomeiwen.com/subject/fadljftx.html