在 TypeScript 中是否有相当于“密封”或“最终”的东西?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42814649/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is there an equivalent to "sealed" or "final" in TypeScript?
提问by bubbleking
I'm trying to implement a method in a super class that should be available for use, but not changeable, in sub classes. Consider this:
我正在尝试在超类中实现一个方法,该方法应该可以在子类中使用,但不可更改。考虑一下:
export abstract class BaseClass {
universalBehavior(): void {
doStuff(); // Do some universal stuff the same way in all sub classes
specializedBehavior(); // Delegate specialized stuff to sub classes
}
protected abstract specializedBehavior(): void;
}
My intention would be that any sub class of BaseClass would not only be free to omit implementation of universalBehavior()
, but not even be allowed to provide an implementation. Is this not (yet) possible in TypeScript? Intellisense complains when I omit the implementation in my sub classes. The best I can seem to do is this:
我的意图是 BaseClass 的任何子类不仅可以自由省略 的实现universalBehavior()
,而且甚至不允许提供实现。这在 TypeScript 中(还)不可能吗?当我省略子类中的实现时,Intellisense 会抱怨。我似乎能做的最好的是:
export class SubClass extends BaseClass {
universalBehavior(): void {
super.universalBehavior();
}
specializedBehavior(): void {
// sub class' implementation
}
}
Obviously this is problematic because I have to ensure that no sub class ever implements universalBehavior()
with anything other than a call to super.universalBehavior()
.
显然这是有问题的,因为我必须确保没有子类universalBehavior()
除了调用super.universalBehavior()
.
采纳答案by bubbleking
No, at the time of this writing there is not. There is a proposal for such a keyword which is still being considered, but may or may not ever be implemented.
不,在撰写本文时还没有。有一个关于这样一个关键字的提议仍在考虑中,但可能会或可能永远不会实施。
See:
看:
回答by Asher Garland
Example of implementation hack of 'sealed method' as readonly property of type function which throws compiler error when attempting to override in extended class:
将“密封方法”作为只读属性的“密封方法”的实现示例,当尝试在扩展类中覆盖时会引发编译器错误:
abstract class BaseClass {
protected element: JQuery<HTMLElement>;
constructor(element: JQuery<HTMLElement>) {
this.element = element;
}
readonly public dispose = (): void => {
this.element.remove();
}
}
class MyClass extends BaseClass {
constructor(element: JQuery<HTMLElement>) {
super(element);
}
public dispose(): void { } // Compiler error: "Property 'dispose' in type 'MyClass' is not assignable to the same property in base type 'BaseClass'"
}
TypeScript 2.0 supports "final" classes through using of private constructor:
TypeScript 2.0 通过使用私有构造函数来支持“final”类:
class A {
private constructor(){}
}
class B extends A{} //Cannot extend a class 'A'. Class constructor is marked as private.ts(2675)
回答by Qwerty Pnk
// I use this workaround:
export default class CreateHandler extends BaseHandler {
// final prop used as method
public readonly create = (blueprint: Blueprint): Response<Record> => {
return blueprint.create();
};
// normal method
public store(blueprint: Blueprint): Response<Record> {
return this.response(blueprint.create());
}
}