AS3:不同接口中的同一变量不可能有 getter 和 setter 吗?
以下代码似乎给编译器带来了歧义(请参阅底部附近注释的错误)。是否不可能在接口之间拆分 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我将以下内容放在一起(与您的代码几乎相同)并且对于 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.
The following is on the maintimeline:
And outputs:
有趣的问题。根据输出,编译器似乎并没有真正理解发生了什么。使用自定义编译器在播放器中强制调用会导致结果
所以答案是否定的。该语言不支持它,运行时也不支持它。这很好,但我永远不想在生产代码中看到它。
编辑:我的测试搞砸了,实际上它在运行时运行良好。
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
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.