About working with custom classes in an application 255
5. In widget_test.fla, type the following code into Frame 1 of the Timeline:
// Before you create any instances of the class,
// Widget.widgetCount is zero (0).
trace("Widget count at start: " + Widget.widgetCount); // 0
var widget1:Widget = new Widget(); // 1
var widget2:Widget = new Widget(); // 2
var widget3:Widget = new Widget(); // 3
trace("Widget count at end: " + Widget.widgetCount); // 3
6.
Save the changes to widget_test.fla.
7. Select Control > Test Movie to test the file.
Flash displays the following information in the Output panel:
Widget count at start: 0
Creating widget # 1
Creating widget # 2
Creating widget # 3
Widget count at end: 3
About getter and setter methods
Getter and setter methods are accessor methods, meaning that they are generally a public
interface to change private class members. You use getter and setter methods to define a
property. You access getter and setter methods as properties outside the class, even though
you define them within the class as methods. Those properties outside the class can have a
different name from the property name in the class.
There are some advantages to using getter and setter methods, such as the ability to let you
create members with sophisticated functionality that you can access like properties. They also
let you create read-only and write-only properties.
Even though getter and setter methods are useful, you should be careful not to overuse them
because, among other issues, they can make code maintenance more difficult in certain
situations. Also, they provide access to your class implementation, like public members. OOP
practice discourages direct access to properties within a class.