AS3:不同接口中的同一变量不可能有 getter 和 setter 吗?

发布于 2024-10-19 04:17:01 字数 757 浏览 7 评论 0原文

以下代码似乎给编译器带来了歧义(请参阅底部附近注释的错误)。是否不可能在接口之间拆分 getter 和 setter?

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.

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

故事与诗 2024-10-26 04:17:01

我将以下内容放在一起(与您的代码几乎相同)并且对于 get 和 set 方法都可以正常工作。

/* 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");
        }
    }
}

以下是主时间线上的内容:

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

和输出:

Get
Set

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
趁年轻赶紧闹 2024-10-26 04:17:01

有趣的问题。根据输出,编译器似乎并没有真正理解发生了什么。使用自定义编译器在播放器中强制调用会导致结果

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

所以答案是否定的。该语言不支持它,运行时也不支持它。这很好,但我永远不想在生产代码中看到它。

编辑:我的测试搞砸了,实际上它在运行时运行良好。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文