TypeScript——类的继承

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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:清华

或者

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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:清华