As you can see,
getUserName
value of
userName
then use the following syntax to get or set the
// calling "get" method
var name = obj.getUserName();
// calling "set" method
obj.setUserName("Jody");
However, if you want to use a more concise syntax, use implicit get/set methods. Implicit get/set
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
function get user():String {
return userName;
}
function set user(name:String):Void {
userName = name;
}
A get method must not take any parameters. A set method must take exactly one required
parameter. A set method can have the same name as a get method in the same scope. Get/set
methods cannot have the same name as other properties. For example, in the example code above
that defines get and set methods named
the same class.
Unlike ordinary methods, get/set methods are invoked without any parentheses or arguments. For
example, the following syntax could now be used to access or modify the value of
the get/set methods defined above.
var name = obj.user;
obj.user = "Jack";
Note: Implicit get/set methods are syntactic shorthand for the Object.addProperty() method
in ActionScript 1.
Creating dynamic classes
By default, the properties and methods of a class are fixed. That is, an instance of a class can't
create or access properties or methods that weren't originally declared or defined by the class. For
example, consider a Person class that defines two properties,
class Person {
var name:String;
var age:Number;
}
If, in another script, you create an instance of the Person class and try to access a property of the
class that doesn't exist, the compiler generates an error. For example, the following code creates a
new instance of the Person class (
, which doesn't exist.
hairColor
var a_person:Person = new Person();
a_person.hairColor = "blue"; // compiler error
This code causes a compiler error because the Person class doesn't declare a property named
. In most cases, this is exactly what you want to happen.
hairColor
returns the current value of
to the string parameter passed to the method. An instance of the class would
and
get
user
a_person
userName
property.
userName
method attributes. You create methods that get or
set
or
before the method name.
get
set
, you could not also have a property named
name
) and then tries to assign a value to a property named
, and
setUserName
userName
and
:
age
Creating dynamic classes
sets the
in
user
with
173
Need help?
Do you have a question about the FLASH MX 2004 - ACTIONSCRIPT and is the answer not in the manual?
Questions and answers