AS2 OOP: Class Structure
         by senocular  

Private and Public
Another addition accompanying ActionScript 2.0 is the ability to create properties and methods as being either private or public using the public and private keywords. With ActionScript 1.0, all properties and methods of a class were entirely public. This means that there was always direct access to a property or method through a class instance. Having private properties and methods means that they would not be accessible through the instance directly, but rather only internally through methods of that class.

The private and public keywords are used in indicating that a property or methods is private or public. They are used in front of the property or method before any other keyword.

private var variableName;
private function functionName(){ ...
public var variableName;
public function functionName(){ ...

If neither is used, public is assumed. This makes the actual public keyword a little redundant. Yet it still may be helpful in distinguishing public from private for a more readable class definition.

Let's return to the Person class and assume all people created with this class will be elusive women. These women will tell you their name, but they won't tell you their age. After all, they want you to think they are at the current point in time (and always will be) 29. Only if you know them well enough, will they divulge their true age. To prevent direct access to these ladies ages though, you would want to make the age property private. Since its ok if anyone knows their names, that property can be assigned to be public.

class Person {
public var name:String = "none";
private var age:Number;
 
function Person(n:String, a:Number) {
name = n;
age = a;
}
 
function revealAge(howWellKnown:Number):Number {
if (howWellKnown > 20) {
return age;
}else{
return 29;
}
}
}

When creating and using these Person instances in your movie, you will not have access to the age property if you attempted to access it directly from the instance directly. Because it's private, a method would be needed to get to that value - a method such as revealAge.

var aLady:Person = new Person("Emily", 36);
 
trace(aLady.name); // "Emily"
trace(aLady.age); // error: cannot access private member
 
trace(aLady.revealAge(10)); // 29
trace(aLady.revealAge(21)); // 36

Try it yourself! (zipped source)

When accessing name, you have no problems since its a public property. The age property, however, is private. When the compiler sees you are trying to access a private property, it throws up an error. Its real value can be attained via methods since they are allowed to access the value of private properties.

Private methods work the very same way. Declaring a function to be private means that the method cannot be used anywhere aside from within the other methods of that class.

private function mySecretMethod():Void {
// contents
}

Even static properties and methods can be private

private static var mySecretProperty:Number;
private static function mySecretMethod():Void {
// contents
}

One important aspect of these private properties and methods is that they only work for strictly typed instances. If you don't use strict typing on your class instance, there will be no error generated by the compiler for private properties and all properties and methods, private or not, will be seen as being public.

When everything comes together, all methods and properties, public or private, static and not, you come up with the following for accessibility.

  Instance Object

Instance
Method

Constructor Object Static
Method
public property Yes Yes No No
public method Yes Yes No No
public  static property No Yes Yes Yes
public  static method No Yes Yes Yes
private property No Yes No No
private method No Yes No No
private static property No Yes No Yes
private static method No Yes No Yes

[ class accessibility chart ]

You can see that instances alone really aren't given that much access. Instance methods, however, have total access. Those methods can access any part of a class no matter what its designation. You can see how methods can really make the class, and its best to program so that this is in fact the case.

 Advanced Programmers Note: Private
Private members to Actionscript are not so much private as they are protected. Subclasses have complete access to all private variables and methods. There is no way to prevent that as of now. The use of private members here is basically just to generate a compiler error when it sees you're using an instance to directly access them.

 

 




SUPPORTERS:

kirupa.com's fast and reliable hosting provided by Media Temple.