测试一下AS3的多态性,准备四个类:MyShape,MyCircle,MyQuare,TestShape(测试类)
输出结果为: "this is MyCircle"
实际应用中MyShape最好写成接口。
package
{
public class MyShape
{
public function drawShape():void{
trace("this is MyShape");
}
}
}
{
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");
}
}
}
{
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");
}
}
}
{
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();
}
}
}
{
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最好写成接口。