|
||||||||||||||||
. . . . . |
Flash ActionScript 2.0 - Class Inheritance
Every class has attributes and behaviours. Attributes can describe the state or qualities of an object:
class Car{
// declare attributes
private var carColor:String;
private var make:String;
private var year:Number;
private var style:String;
}
MethodsMethods define the behaviours of our classes. What do we want our car to do? how about move.. and stop. Lets add them..
class Car{
// declare attributes
private var xpos:Number;
private var ypos:NUmber;
private var spee:Number;
private var carColor:String;
private var make:String;
private var year:Number;
private var style:String;
// Methods
public function move(){
xpos +=speed;
}
public function stop(){
speed = 0;
}
}
Inheritance
Inheritance means that subclasses of a higher class inherit all the methods and data(variables)
from their superclass, or higher class
class Shape{
// declare variables
private var area:Number;
private var posx:Number;
private var posy:Number;
private var speed:Number;
private var direction:String;
// Constructor
public function Shape(xpos:Number, ypos:Number){
posx = xpos;
posy = ypos;
}
// declare methods
public function setArea(parea:Number):Void{
area = parea;
}
public function getArea():Number{
trace("superclass getarea method");
return area;
}
public function move(speed, dir){
if(dir=="up") posy -= speed;
if(dir=="down") posy += speed;
if(dir=="left") posx -= speed;
if(dir=="right") posx += speed;
trace("x = " + posx + ", y = "+posy);
}
}
Now we want a Square class that is a subclass of Shape. Shape is the "base" class and
Square will be the derived class.
class Square extends Shape{
// declare variables
private var width:Number;
// constructor
public function Square(pwidth:Number){
width = pwidth;
}
}
But we know that the area of our square can come from width*width . We could "override" the inherited method getArea(). We could write our own implementation that would do a better job in this class. We will override our getArea method then:
class Square extends Shape{
// declare variables
private var width:Number;
// constructor
public function Square(pwidth:Number){
width = pwidth;
}
// overRidden getArea method
public function getArea():Number{
trace("Square getArea's overridden method");
area = width * width;
return area;
}
}
Now that Square is a subclass of Shape we can use Shape's methods. We can write: var s1:Square = new Square(100); s1.move(); s1.getArea(); PolyMorphism
Which getArea() method did s1 use ? did it use the Shape or Square getArea method?
Answer - the Square class's getArea method.
If a superclass's method is declared and implemented in the derived(sub) class ,
then we say that it has been overwritten. Our circle class
that we will write will have a getArea that returns PI times radius squared. Using the same method name to implement
different code in subclasses is called PolyMorphism. There are many implementations associated with a method depending
upon the object associated with the method name: Write your Circle class which will inherit from the Shape class as well:
class Circle extends Shape{
// declare variables
private var radius:Number;
// constructor
public function Circle(pradius:Number){
radius = pradius;
}
// overRidden getArea method
public function getArea():Number{
trace("inside circle's overridden getarea method");
area = Math.PI* radius* radius;
return area;
}
}
Now test if our square and circle classes implement their own overridden getArea method. var s1:Square = new Square(100); var c1:Circle = new Circle(50); trace(s1.getArea()); trace(c1.getArea());
Output: So polymorphism does work. .. Try out all the other methods . Try and bust it.. bend it.. shake it.. SuperClass Constructors that require Arguments
In our above superclass , Shape , we had a constructor :
class Square extends Shape{
// declare variables
private var width:Number;
// constructor
public function Square(px:Number, py:Number, pwidth:Number){
super(px,py);
width = pwidth;
}
// overRidden getArea method
public function getArea():Number{
trace("Square getArea's overridden method");
area = width * width;
return area;
}
}
And we had better fix up the Circle class too.
class Circle extends Shape{
// declare variables
private var radius:Number;
// constructor
public function Circle(px:Number, py:Number,pradius:Number){
super(px,py);
radius = pradius;
}
// overRidden getArea method
public function getArea():Number{
trace("inside circle's overridden getarea method");
area = Math.PI* radius* radius;
return area;
}
}
And the code in Flash will have to be changed as well.
var s1:Square = new Square(20, 50, 100);
var c1:Circle = new Circle(100, 100, 50);
trace(s1.getArea());
trace(c1.getArea());
// And test to see if Move method works
_root.onEnterFrame = function() {
s1.move( 2, "down");
};
What happened to the x coordinate ? |
|
||||||||||||||
|
| Home | Flash MX | Actionscript 2.0 | Flash 3D | Flash 8 | Flash Database | Flash Mobile | Flash CS3 | Java For Kids | Video Course | General Video | Photoshop | Web Design | Digital Photography | Forum | Games | free backgrounds | Resume | Flash Animations | Streaming Video | Students Work | Links | Contact me | sitemap | reviews | store | advertisers | . . |
||||||||||||||||