ENCAPSULATION
a common way of purging outside info of harmful data is encapsulation, and an example of this is the Sound class getVolume() and setVolume(). for instance, let us say that the person using this class writes in an code outside of the class MyFirstClassInstance.timesSpoken = 1000? this would clearly be bad, since timesSpoken serves as variable to store the number of times the sendMessage() method is called. so instead, we can write a public function to retrieve private data, like this:
public function getTimesSpkn():Number{
return timesSpoken;
}
public function traceLastMsg():Number{
trace("LAST MESSAGE: "+lastMessage);
}
another reason to encapsulate is to monitor or streamline input in a simple way. lets say that you wanted the user to have to run a special method such as setTimesSpkn() in order to have a way allow the user to set a lower number than is currently input. instead of just trusting the user to set the property properly, you can purge the unwanted input in a method, like this:
public function setTimesSpkn(num:Number):Void{
timesSpoken = Math.min(num, timesSpoken);
}
so now, the class we have looks like this:
class MyFirstClass{
public var allCaps:Boolean = false;
public var canTrace:Boolean = true;
private var lastMessage:String = new String("No Messages");
private var timesSpoken:Number = 0;
public function MyFirstClass(startTrace:Boolean, startCaps:Boolean):Void{
allCaps = startCaps;
canTrace = startTrace;
}
public function sendMessage(msg:String):Void{
if (!canTrace) return;
var curMessage:String = allCaps ? msg.toUpperCase() : msg;
trace(curMessage);
lastMessage = curMessage;
timesSpoken++;
}
public function getLastMsg():Number{
trace("LAST MESSAGE: "+lastMessage);
}
public function getTimesSpkn():Number{
return timesSpoken;
}
public function setTimesSpkn(num:Number):Void{
timesSpoken = Math.min(num, timesSpoken);
}
}
APPLYING CLASSES
now, to use this in a FLA (it is assumed you named the *.as file the same as the class name), all you have to do is datatype a variable to the class name. we actually created a constructor to set some starting values to specific variables, so let us fill it out as well.
var myMessenger:MyFirstClass = new MyFirstClass(true, false);
now, we can begin using the class however we would like. lets say that we want to trace a message that says "actionscript is fun". we could call the sendMessage() method:
myMessenger.sendMessage("actionscript is fun");
// output: actionscript is fun
but wait, what if we want to trace those same words in all caps:
myMessenger.allCaps = true;
myMessenger.sendMessage("actionscript is fun");
// output: ACTIONSCRIPT IS FUN
you could even store pieces of encapsulated information to a variable
howManyMessages = myMessenger.getTimesSpkn();
// howManyMessages = 2
EXTENDING A CLASS
one thing to learn at this point is "class extension". this is somewhat of a misnomer, since the terminology "class extension" would make you think that you are adding to the preexisting class. instead, the new class will automatically take on the properties and methods of the extended class.
class xSound extends Sound{
}
when you instantiate the xSound object, you will have access to the entire Sound library, including such methods as setVolume(), getPan(), start(), and stop(), and such properties as position and duration. a neat trick with extending preexisting classes is overwriting old methods. rarely useful, but possible.
the two most common uses for class extensions is to expand on a class by adding more methods that cater to your specific needs or add useful modifications, or to have a movieclip act as a tangible representation of an otherwise abstract object. remember that extended classes have access to any property in the extended class.
IMPLIMENTING CLASSES
for when a custom class needs to have the properties of multiple class, the class must "impliment" the other classes. for instance, if i wanted to create a custom Sound class, and i had a few other basic sound class modification classes i had written, i could impliment the other modification classes and still extend the original Sound class:
class xSound extends Sound impliments SoundModA, SoundModB{
}
this will model the xSound after the Sound, and will include the methods and properties of the SoundModA and the SoundModB classes. this is useful for combining already extended classes to create a versitile class extension.
alright, i hope that was informative. this was merely meant to be a companion guide to go with inglors AS OOP tutorial. feel free to post your class ideas and questions here to get a bit of help. the syntax is different in a few ways to normal AS, so be sure to learn the syntax, and remember: strict variable datatyping is your friend.
- authorblues