@sealed
标签类型: 修饰符
TSDoc 标准化: 扩展
@sealed
修饰符与 C# 中的 sealed
关键字或 Java 中的 final
关键字具有类似的语义。它应该只应用于类或类的成员。
当应用于整个类时,@sealed
修饰符表示该类不能被子类扩展。
当应用于成员时,@sealed
修饰符表示该成员不能被子类的对应成员覆盖(即重新定义)。每当 @sealed
应用于成员时,也必须使用 @override
修饰符。
@sealed
修饰符将显示在生成的文档中。
API 提取器目前不验证 @sealed
修饰符是否被正确使用。(这可能在将来实现。)
用法示例
在这个示例中,@sealed
应用于整个 Button
类
/** @public */
export class Component {
. . .
}
/** @public @sealed */
export class Button extends Component {
. . .
}
/** @public */
export class FancyButton extends Button { // <-- NOT ALLOWED: THE CLASS IS SEALED
. . .
}
在这个示例中,@sealed
应用于成员 Button.componentType
/** @public */
export enum ComponentType {
Control,
Service
}
/** @public */
export abstract class Component {
/** The type of component */
public abstract get componentType(): ComponentType;
/**
* Draws the control on the screen
* @virtual
*/
public render(): void {
. . .
}
}
/** @public */
export class Button extends Component {
/**
* {@inheritDoc Component.componentType}
* @override @sealed
*/
public get componentType(): ComponentType {
return ComponentType.Control;
}
/**
* {@inheritDoc Control.render}
* @override
*/
public render(): void {
. . .
}
}
/** @public */
export class FancyButton extends Button {
/** @override */
public get componentType(): ComponentType { // <-- NOT ALLOWED: THE MEMBER IS SEALED
return ComponentType.Service;
}
/**
* {@inheritDoc Button.render}
* @override
*/
public render(): void { // <-- OKAY
. . .
}
}