开发者

How to define interface method with different arguments in AS3?

I'm trying to define interface in which one method (initPage) needs to have different arguments in every implementation. How can I do this? Using ...args for me is not good, because I'm loosing strong type checking.

public interface IPage {
    function initPage(...args):void;
    function showPage():void;
    function hideP开发者_开发百科age():void;
    function removePage():void;
}


You know you can implements more than one Interface?

I think you should maintain your actual Interface, minus the "problematic" method, and create another Interfaces, each one with a different method

public interface IPage 
{      
    function showPage():void;
    function hidePage():void;
    function removePage():void;
}

public interface IPrettyPage
{
    function initPage(p:PrettyArg);
}

public interface IUglyPage
{
    function initPage(u:UglyArg);
}


// your implementation Class
package 
{
    public class Page extends Sprite implements IPage, IPrettyPage
    {
        // Implementation
    }
}

Anyway, here's an example with another good idea, BUT is a Java code: http://www.java2s.com/Code/Java/Language-Basics/Implementmultipleinterfaces.htm

I didn't tested, but I'm almost sure this issue is different in AS3.


You cannot have an interface that defines a method where each implementation uses different arguments.

The purpose of an interface is to define a common set of method signatures of all classes that implement that interface.

Having each class take different arguments defeats this purpose, so you cannot do it without using rest parameters (...).


Having different arguments for different implementations of an interface goes against the fundamental purpose of an interface. If you only had a reference to the interface, how would you know what arguments to pass?


Using ...args for me is not good, because I'm loosing strong type checking.

so you know the certain amount of arguments and even their types so the only inconvinience i can imagine is that you will often have to call this function with no args. i usually solve it like this: private function doSomething(e: Event = null):void {}

and there's another way if you're not sure about this function - just pass it one object as an argument and change it to some custom associated-array-class as soon as you'll be sure about all data fields


You could use the class constructor to pass any arguments you need and leave the arguments out of the initPage method.

public interface IPage 
{
  function initPage():void;
  function showPage():void;
  function hidePage():void;
  function removePage():void;
}

//any class that implements IPage can have a different
//set of arguments
public class A implements IPage
{
    //for example...
    private var obj1:String;
    private var obj2:int;

    public function A( obj1:String , obj2:int )
    {
       this.obj1 = obj1;
       this.obj2 = obj2;
    }

    public function initPage():void
    {
        //obj1 & ob2 are available here
     }
}


If the different objects that the initPage accept as parameter share a certain structure, they could all extend another interface or parent class. For example having:

function initPage(arg:DisplayObject):void;//For example

The initPage could receive anything like Bitmap, Sprite, or MovieClip objects retaining at least some strict typing. The code within each implementation of the method could at least make sure that such implementation is receiving the expected object type.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜