可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
How can I make an abstract class in AS3 nicely?
I've tried this:
public class AnAbstractClass { public function toBeImplemented():void { throw new NotImplementedError(); // I've created this error } } public class AnConcreteClass extends AnAbstractClass { override public function toBeImplemented():void { // implementation... } }
But.. I don't like this way. And doesn't have compile time errors.
回答1:
回答2:
package { import flash.errors.IllegalOperationError; import flash.utils.getDefinitionByName; import flash.utils.getQualifiedClassName; import flash.utils.getQualifiedSuperclassName; public class AbstractClass { public function AbstractClass() { inspectAbstract(); } private function inspectAbstract():void { var className : String = getQualifiedClassName(this); if (getDefinitionByName(className) == AbstractClass ) { throw new ArgumentError( getQualifiedClassName(this) + "Class can not be instantiated."); } } public function foo():void { throw new IllegalOperationError("Must override Concreate Class"); } } }
package { public class ConcreteClass extends AbstractClass { public function ConcreteClass() { super(); } override public function foo() : void { trace("Implemented"); } } }
回答3:
In AS3 would just use interfaces to make sure all functions are implemented at compile time. I know it different but does the trick for an example such as the one above.
回答4:
As long as they don't permit non-public constructors in actionscript, you'd have to rely on run time errors for abstract classes and singletons.