Flash / Flex / ActionScript/Class/Class — различия между версиями

Материал из Web эксперт
Перейти к: навигация, поиск
м (1 версия)
 
(нет различий)

Версия 09:19, 26 мая 2010

Class Objects

 
package {
  import flash.system.*;
  import flash.display.Sprite;
  public class Main extends Sprite{
    public function Main(){
      var vp = MyClass;
      var pet = new vp();
    }
  }
}

  class MyClass {
    private var pet;
    public function MyClass (  ) {
       trace("constructor");
    }
  }



Convert class to the more general type without writing any explicit code.

 
package{
  import flash.display.Sprite;
  import flash.geom.Rectangle;
  public class Main extends Sprite{
    public function Main(){
        var square:Rectangle = new Rectangle();
        var array:Array = ["hello", "world"];
        var obj:Object;
        while (obj = array.shift())
        {
            trace(obj);
        }
        
        
        var str:String = "";
        if (!str) str = "Default";
        trace(str); //Default
        
        for (var i:int = 1; i < 4; i++)
        {
            trace(i, (i % 2)? "odd" : "even");
        }
    }
  }
}



Define a class

 
package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
        var crTest:Car = new Car();
        trace(crTest.make);  // Displays: undefined
        crTest.make = "Old";
        trace(crTest.make);  // Displays: Olds

    }
  }
}
class Car {
  private var _sMake:String;
  public function get make():String {
    return _sMake;
  }
  public function set make(sMake:String):void {
    _sMake = sMake;
  }
}



Define class with properties

 
package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){

    }
  }
}
class Car {
  private var _sMake:String;
  public function get make():String {
    trace("Getting the value...");
    return _sMake;
  }
  public function set make(sMake:String):void {
    trace("Setting the value...");
    _sMake = sMake;
  }
}