我爱编程

TypeScript——类的继承

2018-05-23  本文已影响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:清华
上一篇下一篇

猜你喜欢

热点阅读