Flash / Flex / ActionScript/Class/Override

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

Invoking an Overridden Instance Method: super.methodName(arg1, arg2, ...argn);

 

class Rectangle {
  protected var w = 0;
  protected var h = 0;
  public function setSize (newW, newH) {
    w = newW;
    h = newH;
  }
  public function getArea (  ) {
    return w * h;
  }
}
class ScreenRectangle extends Rectangle {
  override public function setSize (newW, newH) {
    super.setSize(newW, newH);
    draw(  );
  }
  public function draw (  ) {
  }
}
class ReadOnlyRectangle extends Rectangle {
  override public function setSize (newW, newH) {
  }
}



override method from parent class

 
package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
        
        var c:Cat = new Cat();
        c.speak("Miao");
    }
  }
}
     class Animal {
          public function Animal() {
          }
          public function speak(sound:String):void {
               trace(sound);
          }
     }
     class Cat extends Animal {
          public function Cat() {
          }
          public override function speak(sound:String):void {
               for (var i:uint=0; i < 100; i++) {
                    super.speak("Meow! Meow! Meow!");
               }
          }
     }



Overriding Behavior

 
package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
        var normalGull:Seagull = new Seagull();
        var quietGull:Seagull = new QuietSeagull();
        normalGull.squawk(); //The seagull says &acirc;��SQUAAA!"
        quietGull.squawk(); //...
        
        var politeGull:Seagull = new PoliteSeagull(); //A new seagull appears
        politeGull.eat();
    }
  }
}
     class Seagull
    {
        public function get weight():Number
        {
            return 2;
        }
        public function squawk():void
        {
            trace("The seagull says "SQUAAA!"");
        }
        public function fly():void{
        
        }
        public function eat():void{}
    }
class QuietSeagull extends Seagull
    {
        override public function squawk():void
        {
            trace("...");
        }
    }
     class HungrySeagull extends Seagull
    {
        override public function get weight():Number
        {
            return 1.5;
        }
    }
     class PoliteSeagull extends Seagull
    {
        override public function squawk():void
        {
            super.squawk();
            trace("The shy gull covers his mouth in shame.");
        }
        override public function fly():void
        {
            super.fly();
            trace("The gull lands and apologizes for blocking out the sun.");
        }
        override public function eat():void
        {
            trace("The gull apologizes to the animal it"s about to eat.");
            super.eat();
        }
    }



Overriding Instance Methods

 
 
ActionScript 3.0 allows instance methods to be redefined but not instance variables, static variables, or static methods.

package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
        var aInstance = new A(  );
        aInstance.m();  // Displays: A"s m(  ) was called
        
        var bInstance = new B(  );
        bInstance.m();  // Displays: B"s m(  ) was called
    }
  }
}
class A {
  public function m (  ) {
    trace("A"s m(  ) was called");
  }
}
class B extends A {
  // Override the superclass"s method m(  )
  override public function m (  ) {
    trace("B"s m(  ) was called");
  }
}