当您extend使用类时,可以override使用override关键字在继承的类中定义方法:
public class Example { public function test():void { trace('It works!'); } } public class AnotherExample extends Example { public override function test():void { trace('It still works!'); } }
例:
var example:Example = new Example(); var another:AnotherExample = new AnotherExample(); example.test(); // 输出:有效! another.test(); // 输出:仍然有效!
您可以使用super关键字从继承的类中引用原始方法。例如,我们可以将的主体更改为:AnotherExample.test()
public override function test():void { super.test(); trace('Extra content.'); }
导致:
another.test(); // 输出:有效! // 额外的内容。
重写类的构造函数有点不同。该override关键词被省略和访问继承的构造与简单地完成super():
public class AnotherClass extends Example { public function AnotherClass() { super(); // 在继承的类中调用构造函数。 } }
您还可以重写get和set方法。