类与类型
typescript · intermediate · 15 min · Step 1/2TypeScript Class
interface Printable {
print(): string;
}
class Animal implements Printable {
// 参数属性简写
constructor(
public name: string,
private _age: number,
protected type: string = 'unknown'
) {}
get age(): number { return this._age; }
print(): string {
return `${this.name} (${this.type})`;
}
}
abstract class Shape {
abstract area(): number;
describe() { return `Area: ${this.area()}`; }
}
| 修饰符 | 类内 | 子类 | 外部 |
|---|---|---|---|
| public | ✅ | ✅ | ✅ |
| protected | ✅ | ✅ | ❌ |
| private | ✅ | ❌ | ❌ |