Controlling member access in your classes

By default, any property or method of a class can be accessed by any other class: all members of a class are public by default. However, in some cases you might want to protect data or methods of a class from access by other classes. You need to make those members private (available only to the class that declares or defines them).

You specify public or private members using the public or private member attribute. For example, the following code declares a private variable (a property) and a private method (a function). The following class (LoginClass) defines a private property named userName and a private method named getUserName():

class LoginClass {
  private var userName:String;
  private function getUserName():String {
    return this.userName;
  }
  // Constructor:
  public function LoginClass(user:String) {
    this.userName = user;
  }
}

Private members (properties and methods) are accessible only to the class that defines those members and to subclasses of that original class. Instances of the original class, or instances of subclasses of that class, cannot access privately declared properties and methods; that is, private members are accessible only within class definitions, not at the instance level. In the following example, you change member access in your class files.

NOTE

 

This exercise is part of Example: Writing custom classes. If you do not wish to progress through the example, you can download the class files from www.helpexamples.com/flash/learnas/classes/.

To control member access:

  1. Open ClassA.as and ClassB.as in the Flash authoring tool.
  2. Modify the ClassA.as ActionScript file so its contents match the following ActionScript (the changes to make appear in boldface):
    class com.adobe.utils.ClassA {
        private static var _className:String = "ClassA";
    
        public function ClassA() {
            trace("ClassA constructor");
        }
        public function doSomething():Void {
            trace("ClassA - doSomething()");
        }
    }
    

    This previous code sets both methods (the ClassA constructor and the doSomething() method) as public, meaning that they can be accessed by external scripts. The static _className variable is set as private, meaning the variable can be accessed only from within the class and not from external scripts.

  3. Modify the ClassB.as ActionScript file and add the same method and property access as the ClassA class.
  4. Save both ActionScript files before you proceed.

An instance of ClassA or ClassB cannot access the private members. For example, the following code, added to Frame 1 of the Timeline in a FLA file, would result in a compiler error indicating that the method is private and can't be accessed:

import com.adobe.utils.ClassA;
var a:ClassA = new ClassA();
trace(a._className); // Error. The member is private and cannot be accessed.

Member access control is a compile-time-only feature; at runtime, Flash Player does not distinguish between private or public members.

To continue writing your class file, see Documenting the classes.


Flash CS3


 

Send me an e-mail when comments are added to this page | Comment Report

Current page: http://livedocs.adobe.com/flash/9.0/main/00000793.html