This is working, but is this a good example of polymorphism?
package mtm.test
{
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.MovieClip;
public class TestDocument extends MovieClip
{
public function TestDocument()
{
var image:Image = new Image();
image.id = 0;
image.bitmaps[BitmapFullSize] = new BitmapFullSize(new BitmapData(400, 400, false, 0x000000));
image.bitmaps[BitmapMediumSize] = new BitmapMediumSize(new BitmapData(250, 250, false, 0x000000));
image.bitmaps[BitmapThumbnail] = new BitmapThumbnail(new BitmapData(65, 65, false, 0x000000));
var bmp:BitmapType = image.bitmaps[BitmapFullSize];
if(bmp != null) addChild(bmp);
}
}
}
Here are the classes used above:
package mtm.test
{
import flash.display.Bitmap;
public dynamic class Image extends Object
{
public var id:int;
public var ena开发者_运维知识库bled:int;
public var orderNum:int;
public var filename:String;
public var title:String;
public var description:String;
public var medExists:int;
public var fullExists:int;
public var thumbExists:int;
public var bitmaps:Object = new Object();
}
}
BitmapType Parent Class
package mtm.test
{
import flash.display.Bitmap;
import flash.display.BitmapData;
public class BitmapType extends Bitmap
{
public function BitmapType(bitmapData:BitmapData = null, pixelSnapping:String = 'auto', smoothing:Boolean = false)
{
super(bitmapData, pixelSnapping, smoothing);
}
}
}
Sub-Classes of BitmapType
package mtm.test
{
import flash.display.BitmapData;
public class BitmapFullSize extends BitmapType
{
public function BitmapFullSize(bitmapData:BitmapData = null, pixelSnapping:String = 'auto', smoothing:Boolean = false)
{
super(bitmapData, pixelSnapping, smoothing);
}
}
}
package mtm.test
{
import flash.display.BitmapData;
public class BitmapMediumSize extends BitmapType
{
public function BitmapMediumSize(bitmapData:BitmapData = null, pixelSnapping:String = 'auto', smoothing:Boolean = false)
{
super(bitmapData, pixelSnapping, smoothing);
}
}
}
package mtm.test
{
import flash.display.BitmapData;
public class BitmapThumbnail extends BitmapType
{
public function BitmapThumbnail(bitmapData:BitmapData = null, pixelSnapping:String = 'auto', smoothing:Boolean = false)
{
super(bitmapData, pixelSnapping, smoothing);
}
}
}
It's not really Polymorphism if the children are just calling their parent implementation.
Polymorphism is if each subtype has its own implementation of some method.
So if you had a Resize()
method that employed different a different algorithm based on each type, then that would be polymorphism.
Your code for each of the three subtypes is identical.
Here is a classical example:
Base class is called Vehicle
Subclasses are Car
,Boat
,Airplane
.
Each of Car
,Boat
,Airplane
would have a different Travel
method.
Then you could say:
Vehicle myVehicle = new Boat();
myVehicle.Travel();
That's polymorphism.
精彩评论