However, if you want to use a more concise syntax, use implicit getter/setter methods. Implicit
getter/setter methods let you access class properties in a direct manner, while maintaining good
OOP practice.
To define these methods, use the
set the value of a property, and add the keyword
the following example:
class LoginClass2 {
private var userName:String;
function LoginClass2(name:String) {
this.userName = name;
}
function get user():String {
return this.userName;
}
function set user(name:String):Void {
this.userName = name;
}
}
A getter method must not take any parameters. A setter method must take exactly one required
parameter. A setter method can have the same name as a getter method in the same scope. Getter/
setter methods cannot have the same name as other properties. For example, in the example code
above that defines getter and setter methods named
named
in the same class.
user
Unlike ordinary methods, getter/setter methods are invoked without any parentheses or
arguments. For example, the following syntax could now be used to access or modify the value of
with the getter/setter methods previously defined:
userName
var obj:LoginClass2 = new LoginClass2("RickyM");
// calling "get" method
trace(obj.user);
// calling "set" method
obj.user = "EnriqueI";
trace(obj.user);
Getter/setter method attributes cannot be used in interface method declarations.
Understanding the classpath
In order to use a class or interface that you've defined, Flash must locate the external AS files that
contain the class or interface definition. The list of directories in which Flex searches for class and
interface definitions is called the ActionScript classpath.
By default, the flex_root/WEB-INF/flex/user_classes directory is part of the ActionScript
classpath. In addition, ActionScript classes can be in the same directory as the MXML file. For
more information about including external ActionScript files in Flex applications, see "Using
ActionScript" in Developing Flex Applications.
64
Chapter 2: Creating Custom Classes with ActionScript 2.0
and
method attributes. You create methods that get or
get
set
or
get
set
user
before the method name, as shown in
, you could not also have a property
Need help?
Do you have a question about the FLEX-FLEX ACTIONSCRIPT LANGUAGE and is the answer not in the manual?