Extending a class
Superclass and Subclass
Creating a Superclass
The concept of the Superclass is not new. We have already met it in lesson 4 when we talked about extends. There we referred to the MovieClip superclass, which is present in Flash. Why do we need super- and subclasses? Imagine you have a certain number of objects, which have to be always present in a movie and are used. Now you have a large number of methods/functions, which use these objects. We can create one flie of course, which will be very long or we create a superclass, which introduces the objects and various subclasses with individual methods. The advantage is that if we want to extend the superclass by adding another method we just create another subclass and do not have to touch any of the other files at all. In the following example we create our own super- and subclasses. Let's first look what we make. In the movie below enter a number and then press one of the buttons and see what happens.
We have two methods here: the getURL method invoked by the upper button and a method whereby the lesson titles are shown in a textfield. In common are this textfield and the textinput field. So in our superclass we put scripts to create these two objects. But we also put arrays to refer to from outside the class as shown below. We call our superclass OOPtutorial. As you can see we made two arrays, which hold all the strings we need.,
class OOPtutorial {
var a_oop:Array; //IMPORTANT: define both var here!
var a_title:Array;
public function OOPtutorial () {
}
public function superClass ():Void {
//loading the textinput
_root.createClassObject(mx.controls.TextInput, "inText", 2, {});
_root.inText._x = 25;
_root.inText._y = 60;
_root.inText._width = 25;
//creating a textfield for the textinput
_root.createTextField("l_text",3,55,60,100,20);
_root.l_text.text = "Enter a number.";
//creating the main common textfield
_root.createTextField("title_text",5,15,140,180,20);
a_oop = new Array();
a_oop = ["lesson1.html","lesson2.html","lesson3.html","lesson4.html",
"lesson5.html","lesson6.html","lesson7.html","lesson8.html"];
a_title = new Array();
a_title = ["MX versus MX 2004:Bugs","The first external Actionscript",
"Public, Private, static","Extend the class","Getter and Setter",
"packages: Interface - implements","Folders and Paths","Superclass"];
}
}
Next we have to define the subclasses Get_url and Show_title. Let's look at Get_url first:
class Get_url extends OOPtutorial {
var a_oop:Array;
public function Get_url () {
}
public function lesson_url ():Void {
//we show super here
super();
//we execute the superClass function from OOPtutorial here
//refering with "this" to the superclass.
this.superClass();
//for later purpose we redefine "this" and put it into a var
var thisSup:OOPtutorial = this;
//we create a button we need for this method
_root.createClassObject(mx.controls.Button, "lesBut", 1, {label:"Load lesson"});
_root.lesBut._x = 15;
_root.lesBut._y = 15;
//we give the button a function
var myButListener:Object;
myButListener = new Object();
myButListener.click = function () {
//the input from the textinput...
var s_myNum:String = _root.inText.text;
//..is converted to a number.
var n_myNum:Number = parseInt(s_myNum);
//several statements to make sure the user does it correctly.
if (s_myNum == "") {
_root.title_text.text = "Enter a number.";
} else {
if (n_myNum < 1 || n_myNum > thisSup.a_oop.length) {
_root.title_text.text = "No lesson "+ n_myNum;
} else {
//here we refer to the array "a_oop" from the superclass
getURL(thisSup.a_oop[n_myNum-1],"_blank");
}
if (s_myNum == undefined) {
_root.title_text.text = "Enter a number.";
}
}
}
_root.lesBut.addEventListener("click", myButListener );
}
}
We use the word extends to show that this is a subclass of our superclass OOPtutorial. We then have to define the var used in the superclass and we do that directly after the class definition. In our main function "lesson_url" we first indicate there is a superclass by super();. We don't have to since Flash is doing this for us but I prefer to do it. Then we need to execute the superClass function from OOPtutorial. We always use "this" to refer to the superclass. The rest is explained in the script.
The second method encoded in the subclass "Show_title" is very similar.
class Show_title extends OOPtutorial {
var a_title:Array;
public function Show_title () {
}
public function sh_title ():Void {
super();
this.superClass();
var thisSup:OOPtutorial = this;
_root.createClassObject(mx.controls.Button, "titBut", 4, {label:"Show title"});
_root.titBut._x = 15;
_root.titBut._y = 100;
var myButListener:Object;
myButListener = new Object();
myButListener.click = function () {
var s_myNum:String = _root.inText.text;
var n_myNum:Number = parseInt(s_myNum);
if (s_myNum == "") {
_root.title_text.text = "Enter a number.";
} else {
if (n_myNum < 1 || n_myNum > thisSup.a_oop.length) {
_root.title_text.text = "No lesson "+ n_myNum;
} else {
_root.title_text.text = thisSup.a_title[n_myNum-1];
}
}
}
_root.titBut.addEventListener("click", myButListener );
}
}
In our fla file we add these couple of lines to execute both methods. Do not forget to put the button and textinput into the library of your fla, it won't work otherwise.
var g:Get_url = new Get_url(); g.lesson_url(); var t:Show_title = new Show_title(); t.sh_title();
Next we will look at the dynamic class.