Classes (ex. 08)

  1. Create a class called MyClass, and declare the following three variables (pay attention to the underscore in 2 and 3 :
    • myVar1
    • _myVar2
    • _myVar3
  2. Set myVar1 to public, and _myVar2 and _myVar3 to private.
  3. Save the class under the name "MyClass.as".
  4. Create a Flash document and make an object called testClass of the type MyClass.
  5. Save the document in the same folder as the class file.
  6. In the Flash document, set the value of myVar1 to 100.
  7. Trace it to the output panel.
  8. Try to set the value of _myVar2 or _myVar3 to see the private modifier in effect.
  9. Open MyClass.as and create a function called getVar2() and setVar2().
  10. Make them either set the value of _myVar2 or return the value of _myVar2
  11. Open the flash document and use setVar2() to set the value to 200
  12. Use getVar2() to trace the value of the private property.
  13. Create a getter/setter for _myVar3 called myVar3.
  14. Set the value of _myVar3 via it's setter to 300, and use the getter to trace it to the output panel.

Reference

Basics of object-oriented programming

Classes

Classes > Variables

Get and Set accessor methods