ActionScript 3 的多态性
测试一下AS3的多态性,准备四个类:MyShape,MyCircle,MyQuare,TestShape(测试类)
package
...{
public class MyShape
...{
public function drawShape():void...{
trace("this is MyShape");
}
}
}package
...{
public class MyCircle extends MyShape
...{
override public function drawShape():void...{
trace("this is MyCircle");
}
}
}package
...{
public class MySquare extends MyShape
...{
override public function drawShape():void...{
trace("this is MySqure");
}
}
}package
...{
import flash.display.Sprite;
public class TestShape extends Sprite
...{
public function TestShape()
...{
newShape(new MyCircle());
}
public function newShape(myShape:MyShape):void...{
myShape.drawShape();
}
}
}
输出结果为: "this is MyCircle"
实际应用中MyShape最好写成接口。
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/lixinye0123/archive/2007/08/24/1758128.aspx