Sunday 9 January 2011

adobe flex, as3.0, getters and setters

in the follow code, the internalName is the internal variable of the class and the name is the variable that works like property of the class.

---full version--- this version is... human redable
  private var internalName:String;
  public function get name():String {
   return internalName;
  }
  public function set name(newName:String):void {
   internalName=newName;
  } 

---compressed version--- this is nothing more than all previous code in one line
private var internalName:String;   public function get name():String {return internalName;}   public function set name(newName:String):void {internalName=newName; }

--rapid develop with excel--- if you are going to develop so many getters and setters, use this excel formula, replacing the A1 with the cell address that has the name of the property (like the name previously)
="    /** getter/setter "&A1&" */   private var internal"&A1&":String;   public function get "&A1&"():String {return internal"&A1&";}   public function set "&A1&"(new"&A1&":String):void {internal"&A1&"=new"&A1&"; }"

examples
pet.name = "Andreas";
trace("pet.name");

Note: A get/set pseudo-variable can be made read-only by declaring a get method without declaring a set method.

No comments:

Post a Comment