为什么属性在接口中不可声明
在 Actionscript 3 中,我无法在接口中声明变量。 我不明白。 我知道我可以通过定义 getter 和 setter 来解决这个问题,但是如果我只想要一个简单的公共财产怎么办? 如果我设置或获取属性时需要做一些事情,我通常会使用 getter 和 setter,但是如果我只想存储一个值怎么办?
In Actionscript 3 I can't declare vars in Interfaces.
I don't get it.
I know i can work this around by defining getters and setters, but what if i just want a simple public property? I usually use getters and setters if there is something to do when i set or get a property, but what if i just want to store a value?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你可以这样说:接口的存在是因为在你的语言中你不能从多个抽象基类继承。 如果 AS3 允许您这样做,它可能不会有“接口”,而是“纯抽象类”。
换句话说,在接口中实现属性实现会导致名称冲突,并由此导致其他多重继承问题(菱形)。
然而,只有一个 getter 或 setter 而不进行任何实现应该是可行的。
(我手头没有AS3编译器)
You can put it like this: interfaces exist because in your language you can't inherit from multiple abstract base classes. If AS3 would have allowed you to do that, it would probably not have 'interfaces', but 'pure abstract classses'.
In other words, having properties implementation in your interface would lead to name clashes and from there to other multiple inheritance problems (diamond).
However, having just a getter or a setter with no implementation should work.
(I don't have the AS3 compiler at hand)
我不是一个actioscript编程,但是接口(例如在java中)是为了定义行为而不是状态,所以jave中的接口只是声明实现接口的类需要定义的方法。 属性(或实例变量)通常不是定义行为所必需的,并且在接口中也是不允许的。
I'm not an actioscript programming, but interfaces (in java for example) are meant to define behavior not state, so intrerfaces in jave simply declare methods that the class implemeting the interface needs to define. Properties (or instance variable) are not in general necessary to define behavior and there are disallowed in interfaces.