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

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

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

Creating Interfaces

 
interface InterfaceName {
  // Interface code goes here.
}

interface IMessageSender {
  private var _msToSend:Message;  // Incorrect.
}



Extends an interface

 
interface ISender {
  public function send():void;
}
interface IMessageSender extends ISender {
  public function setMessage(msObject:Message):void;
}



Implement an Interface

 
package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
        var srcInstance:ShapeContainer = new ShapeContainer(200, 200);
        
        var cirA:Circle = new Circle(0, 0, 50);
        var cirB:Circle = new Circle(0, 0, 400);
        var sqA:Square = new Square(0, 0, 100);
        var sqB:Square = new Square(0, 0, 200);
        
        var bAdded:Boolean;
        
        bAdded = srcInstance.addShape(cirA);
        trace(bAdded);
        bAdded = srcInstance.addShape(cirB);
        trace(bAdded);
        bAdded = srcInstance.addShape(sqA);
        trace(bAdded);
        bAdded = srcInstance.addShape(sqB);
        trace(bAdded);
    }
  }
}
interface IShape {
  function getDimensions():Object;
}
class Circle implements IShape{
  private var _nX:Number;
  private var _nY:Number;
  private var _nRadius:Number;
  public function Circle(nX:Number, nY:Number, nRadius:Number) {
    nX = nX;
    nY = nY;
    nRadius = nRadius;
  }
  public function getDimensions():Object {
    return {width: 2* _nRadius, height: 2  * _nRadius};
  }
}
class Square implements IShape {
  private var _nX:Number;
  private var _nY:Number;
  private var _nSide:Number;
  public function Square(nX:Number, nY:Number, nSide:Number) {
    _nX = nX;
    nY = nY;
    nSide = nSide;
  }
  public function getDimensions():Object {
    return {width: _nSide, height: _nSide};
  }
}
class ShapeContainer {
  private var _aShapes:Array;
  private var _nWidth:Number;
  private var _nHeight:Number;
  public function ShapeContainer(nWidth:Number, nHeight:Number) {
    _nWidth = nWidth;
    _nHeight = nHeight;
    _aShapes = new Array();
  }
  public function addShape(shObject:IShape):Boolean {
    if(shObject.getDimensions().height > _nHeight) {
      return false;
    }
    var nSum:Number = 0;
    for(var i:Number = 0; i < _aShapes.length; i++) {
      nSum += _aShapes[i].getDimensions().width;
    }
   
    if(nSum + shObject.getDimensions().width > _nWidth) {
      return false;
    }
    else {
      _aShapes.push(shObject);
      return true;
    }
    }
}



Implementing an Interface

 
class ClassName implements InterfaceName {
  // Class code goes here.
}
class ClassName implements Interface1Name, Interface2Name, Interface3Name {
  // Class code goes here.
}
class ClassName extends SuperClassName implements InterfaceName {
  // Class code goes here.
}



Implement interface

 
class Didgeridoo implements IProduct {
          
    private var _price:uint = 100;
    private var _name:String = "Handpainted Didgeridoo";
    private var _description:String = "Imported from Australia";
    public function get price():uint {
      return this._price;
    }
    public function set price(val:uint):void {
      this._price = val;
    }
    public function get name():String {
      return this._name;
    }
    public function set name(val:String):void {
      this._name = val;
    }
    public function get description():String {
      return this._description;
    }
    public function set description(val:String):void {
      this._description = val;
    }
  }
     
interface IProduct {
          function get price():uint;
          function set price(val:uint):void;
          function get name():String;
          function set name(val:String):void;
          function get description():String;
          function set description(val:String):void;
}

class TShirt implements IProduct {
    private var _price:uint = 20;
    private var _name:String = "T-Shirt";
    private var _description:String = "Made in China";
    public function get price():uint {
      return this._price;
    }
    public function set price(val:uint):void {
      this._price = val;
    }
    public function get name():String {
      return this._name;
    }
    public function set name(val:String):void {
      this._name = val;
    }
    public function get description():String {
      return this._description;
    }
    public function set description(val:String):void {
      this._description = val;
    }
  }



Implement two interface

 
package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
       var t:TShirt = new TShirt();
       trace(t.name);
        
        
    }
  }
}
interface IClothing extends IProduct {
          function get color():String;
          function set color(val:String):void;
          function get size():String;
          function set size(val:String):void;
     }
interface ICustomizable {
          function get customPrint():String;
          function set customPrint(val:String):void;
     }
interface IProduct {
          function get price():uint;
          function set price(val:uint):void;
          function get name():String;
          function set name(val:String):void;
          function get description():String;
          function set description(val:String):void;
     }
class TShirt implements IClothing, ICustomizable {
    private var _price:uint = 20;
    private var _name:String = "T-Shirt";
    private var _description:String = "Made in China";
    private var _color:String = "black";
    private var _size:String = "XXL";
    private var _customPrint:String = "";
    public function get price():uint {
      return this._price;
    }
    public function set price(val:uint):void {
      this._price = val;
    }
    public function get name():String {
      return this._name;
    }
    public function set name(val:String):void {
      this._name = val;
    }
    public function get description():String {
      return this._description;
    }
    public function set description(val:String):void {
      this._description = val;
    }
    public function get color():String {
      return this._color;
    }
    public function set color(val:String):void {
      this._color = val;
    }
    public function get size():String {
      return this._size;
    }
    public function set size(val:String):void {
      this._size = val;
    }
    public function get customPrint():String {
      return this._customPrint;
    }
    public function set customPrint(val:String):void {
      this._customPrint = val;
    }
  }



Interface Inheritance

 
public interface IntA {
  function methodA ( ):void;
}
public interface IntB extends IntA {
  function methodB ( ):void;
}
public interface IntC {
  function methodC ( ):void;
}
public interface IntD {
  function methodD ( ):void;
}
public interface IntE extends IntC, IntD {
  function methodE ( ):void;
}



Interface Syntax and Use

 
interface SomeName {
  function method1 (param1:datatype,...paramn:datatype):returnType;
  function method2 (param1:datatype,...paramn:datatype):returnType;
  ...
  function methodn (param1:datatype,...paramn:datatype):returnType;
}
class SomeName implements SomeInterface {
}
A class can legally implement more than one interface by separating interface names with commas.
class SomeName implements SomeInterface, SomeOtherInterface {
}