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

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

Текущая версия на 08:14, 26 мая 2010

If you want the toString( ) method of Subclass to return a different value, you"ll need to override it in the subclass

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



Implementing Subclass Versions of Superclass Methods

 
By default, Subclass inherits the toString( ) method as it"s implemented in Superclass:
package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
        var example:Subclass = new Subclass(  );
        
        trace(example.toString(  )); // Displays: Superclass.toString(  )
    }
  }
}
class Superclass {
        public function Superclass(  ) {}
        public function toString(  ):String {
            return "Superclass.toString(  )";
        }
    }
class Subclass extends Superclass {
        public function Subclass(  ) {}
}



Polymorphism Demo

 
  class UnpaidIntern extends Employee {
    override public function receivePayment():Number {
      return 0;
    }
  }
  class Manager extends Employee {
  
    override public function receivePayment():Number {
      return baseSalary*3;
    }
  }
  class Engineer extends Employee {
    override public function receivePayment():Number {
      return this.baseSalary*2;
  }
  }
  class Employee {
    internal var baseSalary:Number = 1000;
    public function receivePayment():Number {
      return this.baseSalary;
    }
  }