类-2
Tutorial: TS初级二
Category: TS
Published: 2026-04-07 13:58:26
Views: 20
Likes: 0
Comments: 0
- 抽象类
export default {};
abstract class Person {
abstract name: string;
abstract show(): string;
showName() {
console.log(this.show());
}
}
class Student extends Person {
name: string = "孟子义";
age: number = 18;
show(): string {
return "陈情令";
}
}
let s = new Student();
let res = s.show();
console.log(res);
- implements 子句
export default {};
interface IPersonInfo {
name: string;
age: number;
sex?: string;
show(): void;
}
interface IMusic {
music: string;
}
class Person implements IPersonInfo {
name: string = "吴谨言";
age: number = 32;
music: string = "雪落下的声音";
show(): void {
console.log(`${this.name}是延禧攻略的主演, 她今年${this.age}岁了`);
console.log(`${this.name}唱了一首歌叫${this.music}`);
}
}
let p = new Person();
p.show();
interface ITest extends Person {
salary: number;
}
class Star extends Person implements ITest {
salary: number = 50;
name: string = "关晓彤";
age: number = 18;
}
let s = new Star();
console.log(s.salary);
console.log(s.name);