User Guide
120 Object-Oriented Programming in ActionScript
{
return this;
}
}
var myTest:ThisTest = new ThisTest();
trace(myTest.thisValue() == myTest); // true
Inheritance of instance methods can be controlled with the keywords override and final.
You can use the
override attribute to redefine an inherited method, and the final attribute
to prevent subclasses from overriding a method. For more information, see “Overriding
methods” on page 134.
Get and set accessor methods
Get and set accessor functions, also called getters and setters, allow you to adhere to the
programming principles of information hiding and encapsulation while providing an easy-to-
use programming interface for the classes that you create. Get and set functions allow you to
keep your class properties private to the class, but allow users of your class to access those
properties as if they were accessing a class variable instead of calling a class method.
The advantage of this approach is that it allows you to avoid the traditional accessor functions
with unwieldy names, such as
getPropertyName() and setPropertyName(). Another
advantage of getters and setters is that you can avoid having two public-facing functions for
each property that allows both read and write access.
The following example class, named GetSet, includes get and set accessor functions named
publicAccess() that provide access to the private variable named privateProperty:
class GetSet
{
private var privateProperty:String;
public function get publicAccess():String
{
return privateProperty;
}
public function set publicAccess(setValue:String):void
{
privateProperty = setValue;
}
}
If you attempt to access the property privateProperty directly, an error will result, as
follows:
var myGetSet:GetSet = new GetSet();
trace(myGetSet.privateProperty); // error occurs