Flash / Flex / ActionScript/Class/Inheritance

Материал из Web эксперт
Версия от 08:14, 26 мая 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

Add new method to the children class

 

package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
        var bInstance = new B(  );
        bInstance.m();       // Displays: Method m(  ) was called
        bInstance.n();       // Displays: Method n(  ) was called
        trace(bInstance.v);  // Displays: 10

    }
  }
}
class A {
  public var v = 10;
  public function m (  ) {
    trace("Method m(  ) was called");
  }
}
class B extends A {
  public function n (  ) {
    trace("Method n(  ) was called");
  }
}
Method m(  ) was called
Method n(  ) was called
10



A Primer on Inheritance

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

        var aInstance = new A(  );
        aInstance.m();       // Displays: Method m(  ) was called
        trace(aInstance.v);  // Displays: 10
        var bInstance:B = new B(  );
        bInstance.m();       // Displays: Method m(  ) was called
        trace(bInstance.v);  // Displays: 10
        
    }
  }
}
class A {
  public var v = 10;
  public function m (  ) {
    trace("Method m(  ) was called");
  }
}
class B extends A {
}
Method m(  ) was called
10
Method m(  ) was called
10



Call super constructor with parameter

 

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

    }
  }
}
class Ball {
  private var r;
  public function Ball (radius) {
    r = radius;
  }
}

class ColoredBall extends Ball {
  private var c;
  public function ColoredBall (radius, color) {
    super(radius);
    c = color;
  }
}



Class Scope: Code placed at the top-level of a class body resides in that class"s scope.

 
package {
  public class SomeClass {
    // Code here is in the someClass scope
  }
}
package {
  public class SomeClass extends SomeParentClass {
  }
}
package {
  public class SomeParentClass {
  }
}



Constructor Methods in Subclasses

 

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

    }
  }
}
class A {
  public function A (  ) {
  }
}
class B extends A {
  public function B (  ) {
    // Invoke superclass"s constructor method
    super(  );
  }
}



Extends the super class

 
/*
class ClassName extends SuperClassName {
  // Class definition goes here.
}
*/

package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
        var rBunny:Rabbit = new Rabbit("William", "White and Brown");
        
        trace(rBunny.name);  // Displays: William
        trace(rBunny.coloration);  // Dislays: White and Brown
    }
  }
}
class Mammal {
  var _sName:String;
  public function get name():String {
    return _sName;
  }
  public function set name(sName:String):void {
    _sName = sName;
  }
}
class Rabbit extends Mammal {
  public var _sColoration:String;
  public function Rabbit(sName:String, sColoration:String) {
    _sName = sName;
    _sColoration = sColoration;
  }
  public function get coloration():String {
    return _sColoration;
  }
}



If a subclass does not define a constructor, ActionScript automatically creates one and adds a super call as its only statement.

 
class B extends A {
  public function B (  ) {
    super(  );
  }
}
class B extends A {
}

class Rectangle {
  protected var w = 0;
  protected var h = 0;
  public function Rectangle (width, height) {
    setSize(width, height);
  }
  public function setSize (newW, newH) {
    w = newW;
    h = newH;
  }
  public function getArea (  ) {
    return w * h;
  }
}
class Square extends Rectangle {
  public function Square (side) {
    super(side, side);
  }
  override public function setSize (newW, newH) {
    if (newW == newH) {
      super.setSize(newW, newH);
    }
  }
}



Static Methods and Static Variables Not Inherited

 

package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
        var bInstance = new B(  );
        bInstance.s(); // Error
    }
  }
}
class A {
  public static function s (  ) {
    trace("A.s(  ) was called");
  }
}
class B extends A {
  public function B (  ) {
  }
}



Three-level inheritance

 
package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
        var cInstance = new C(  );
        cInstance.m();  // Displays: Method m(  ) was called
        cInstance.n();  // Displays: Method n(  ) was called
        cInstance.o();  // Displays: Method o(  ) was called
        trace(cInstance.v);  // Displays: 10
    }
  }
}
class A {
  public var v = 10;
  public function m (  ) {
    trace("Method m(  ) was called");
  }
}
class B extends A {
  public function n (  ) {
    trace("Method n(  ) was called");
  }
}
class C extends B {
  public function o (  ) {
    trace("Method o(  ) was called");
  }
}



You can call the superclass implementation from the subclass implementation using the super keyword to reference the superclass:

 
    class Superclass {
        public function Superclass(  ) {}
        public function toString(  ):String {
            return "Superclass.toString(  )";
        }
    }
    class Subclass extends Superclass {
        public function Subclass(  ) {}
        override public function toString(  ):String {
            super.toString();
            return "Subclass.toString(  )";
        }
    }