EasyManua.ls Logo

MACROMEDIA FLASH 8-LEARNING ACTIONSCRIPT 2.0 IN FLASH - Page 262

MACROMEDIA FLASH 8-LEARNING ACTIONSCRIPT 2.0 IN FLASH
830 pages
Print Icon
To Next Page IconTo Next Page
To Next Page IconTo Next Page
To Previous Page IconTo Previous Page
To Previous Page IconTo Previous Page
Loading...
262 Classes
An example of encapsulation in Flash would be setting all your member and class variables to
private and forcing people who implement your classes to access these variables using getter
and setter methods. Performing encapsulation this way ensures that if you ever need to change
the structure of the variables in the future, you would need only to change the behavior of the
getter and setter functions rather than force every developer to change the way he or she
accesses the classs variables.
The following code shows how you could modify the Person class from earlier examples, set
its instance members to private, and define getter and setter methods for the private instance
members:
class Person {
private var __userName:String;
private var __age:Number;
public function get userName():String {
return this.__userName;
}
public function set userName(value:String):Void {
this.__userName = value;
}
public function get age():Number {
return this.__age;
}
public function set age(value:Number):Void {
this.__age = value;
}
}
About using the this keyword in classes
Use the this keyword as a prefix within your classes for methods and member variables.
Although it is not necessary, the
this keyword makes it easy to tell that a property or method
belongs to a class when it has a prefix; without the keyword, you cannot tell whether the
property or method belongs to the superclass.
You can also use a class name prefix for static variables and methods, even within a class. This
helps qualify the references you make, which makes code readable. Depending on the coding
environment you use, adding prefixes might also trigger code hints.
NOTE
You do not have to add these prefixes, and some developers feel it is unnecessary.
Macromedia recommends adding the
this keyword as a prefix, because it can aid
readability and helps you write clean code by providing context for your methods and
variables.

Table of Contents

Related product manuals