- 抽象类是专门用于定义那些不希望被外界直接创建的类的
- 抽象类一般用于定义基类
- 抽象类和接口一样用于约束子类
错误示例:
abstract class Person {
abstract name: string;
abstract say(): void;
}
class Student extends Person {
name: string = 'BNTang';
say(): void {
console.log(`我的名字是${this.name}`);
}
}
let p = new Person();
正确示例:
abstract class Person {
abstract name: string;
abstract say(): void;
}
class Student extends Person {
name: string = 'BNTang';
say(): void {
console.log(`我的名字是${this.name}`);
}
}
let stu = new Student();
stu.say();
抽象类和接口的区别
- 接口中只能定义约束, 不能定义具体实现
- 而抽象类中既可以定义约束, 又可以定义具体实现
abstract class Person {
abstract name: string;
abstract say(): void;
eat(): void {
console.log(`${this.name}正在吃东西`);
}
}
class Student extends Person {
name: string = 'BNTang';
say(): void {
console.log(`我的名字是${this.name}`);
}
}
let stu = new Student();
stu.say();
stu.eat();
|
请发表评论