开发者

AS3: Not possible to have getters and setters for same variable in different interfaces?

The following code seems to create an ambiguity for the compiler (please see error commented near the bottom). Is it not possible to have getters and setters split between interfaces?

public interface GetterInterface 
{
    function get 开发者_运维问答test():int;
}

public interface SetterInterface 
{
    function set test(value:int):void;
}

public interface SplitTestInterface extends GetterInterface, SetterInterface
{

}

public class SplitTest implements SplitTestInterface
{

    public function SplitTest() 
    {

    }

    /* INTERFACE test.SetterInterface */

    public function set test(value:int):void 
    {

    }

    /* INTERFACE test.GetterInterface */

    public function get test():int 
    {
        return 0;
    }

}

//Somewhere...
var splitTest:SplitTestInterface = new SplitTest();
splitTest.test = 2; //Error: Property is read-only.


I put together the following (which is all but identical to your code) and works fine for both the get and set method.

/* IGet.as */
package {
    public interface IGet 
    {
        function get test():int;
    }
}


/* ISet.as */
package {
    public interface ISet 
    {
        function set test(i:int):void;
    }
}


/* ISplit.as */
package {
    public interface ISplit extends IGet, ISet {
    }
}

/* SplitTest.as */
package {
    public class SplitTest implements ISplit {

        public function SplitTest() {
        }

        public function set test(i:int):void {
            trace("Set");
        }

        public function get test():int {
            trace("Get");
        }
    }
}

The following is on the maintimeline:

var foo:SplitTest = new SplitTest();
foo.test;
foo.test = 1;

And outputs:

Get
Set


Interesting question. Based on the output, it looks like the compiler doesn't really understand what's going on. Using a custom compiler to call force the call in the player results in

Property SplitTestInterface::test not found on SplitTest and there is no default value.

So the answer is no. It's not supported by the language, and it's not supported by the runtime. That's good though, I would never want to see this in production code.

Edit: My test was messed up, actually it works fine in the runtime.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜