Flash Components      Flash Menu      Flash Gallery      Flash Slideshow      FLV Player      Flash Form      MP3 Player      PhotoFlow      Flash CMS      3D Wall      Flash Scroller

Flash / AS

Silverlight

WPF

ASP.net / PHP

Photoshop

Forums

Blog

About

 


FlashComponents
  Galleries
  Slideshows
  Menus
  Design & Effects
  Audio & Video
  User Interface
  Templates

 

 

 

 

 
AS2 OOP: Class Structure
         by senocular

Interfaces
In the continuing saga of ActionScript 2.0's attempts to make us good and organized OO programmers, we get blessed with the presence of interfaces. Interfaces, though defined like classes, are not themselves classes (starting to see a trend?). They provide programmers with a set of method declarations that are designated required methods for all and any classes that implements that interface. Like classes, interfaces are defined in separate .as files which bears the same name as the interface itself.

interface InterfaceName { /* interface code block */ }

The contents of the interface is then set up much like an intrinsic class is, providing declarations but no definitions. However, interfaces only include public methods. Properties nor static or private declarations are allowed. Here is a example of an interface which outlines methods that are required for "simple fighting styles."

interface SimpleFightingStyle {
function headButt():Boolean;
function kick(foot:String):Boolean;
function block():Void;
}

The new implements keyword is then used to force a class to contain these definitions. Its placed after the class name as so.

class className implements InterfaceName { ...

Here is an example of a class that implements the SimpleFightingStyle interface from above.

class FookYoo implements SimpleFightingStyle {
static var origin:String = "Scotland";
var level:Number = 0;
 
function FookYoo(lvl:Number) {
level = lvl;
}
 
function headButt():Boolean { // required
var success = (Math.random()*10 > 4) ? true : false;
return success;
}
function kick(foot:String):Boolean { // required
var success = false;
if (foot == "right") {
success = (Math.random()*10 > 2) ? true : false;
}else if (foot == "left") {
success = (Math.random()*10 > 4) ? true : false;
}
return success;
}
function block():Void { // required
if (Math.random()*10 > 5) {
preventDamage();
}
}
 
function preventDamage():Void {
// ...
}
}

If that class didn't contain a method for each of those named in the interface being implemented, an error would occur. By implementing an interface, you can be assured that you (or anyone else that might possibly be implementing an interface of yours) don't leave out a method that's necessary for that class. Its advantages are best seen when used with many classes. This helps maintain consistency.

[ interface methods required in the classes implementing it ]

Such classes aren't restricted to using interface methods alone, however. Aside from the methods declared in the interface, the class can also have whatever other methods it needs to have to be what it needs to be. Interfaces just make sure you have at least what they provide for you.

Also, don't forget that interfaces are their own data type.

var skill:FookYoo = new FookYoo(3);
trace(skill instanceof SimpleFightingStyle); // true
 
var skill:SimpleFightingStyle = new FookYoo(3); // also acceptable

Try it yourself! (zipped source)

Some say that interfaces allow for a type of limited multiple inheritance. Don't take this literally. That is not at all what they provide. Inheritance is the act of gaining functionality from another object which already possesses that functionality. Interfaces only make sure that classes define for themselves methods of similar naming. It does not require for those methods to behave the same in any way or mean that anything aside from consistent naming is provided for those classes. Interfaces are more of an enforcer of polymorphism. Really, it's nothing more than a tool to help you do things the way you intended to do them - to help you to main consistency throughout common classes which need to share a similar method structure. Interfaces serve no real purpose otherwise - no killer functionality or simplification of techniques; just a plain ordinary "Ah! but you forgot this!"

 


kirupa.com's fast and reliable hosting provided by Media Temple. flash components
The Text Animation Component for Flash CS3
Check out the great, high-quality flash extensions. Buy or sell stock flash, video, audio and fonts for as little as 50 cents at FlashDen.
Check out our high quality vector-based design packs! Flash Effect Components
flash menus, buttons and components Digicrafts Components
The best flash components ever! Entheos Flash Website Templates
Upload, publish, deliver. Secure hosting for your professional or academic video, presentations & more. Screencast.com Purchase & Download Flash Components
flash components Free Website | Make a Website
Streamsolutions Content Delivery Networks Learn how to advertise on kirupa.com