为什么属性在接口中不可声明

发布于 2024-07-29 22:18:55 字数 163 浏览 3 评论 0原文

在 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 技术交流群。

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

发布评论

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

评论(2

心房敞 2024-08-05 22:18:56

你可以这样说:接口的存在是因为在你的语言中你不能从多个抽象基类继承。 如果 AS3 允许您这样做,它可能不会有“接口”,而是“纯抽象类”。

换句话说,在接口中实现属性实现会导致名称冲突,并由此导致其他多重继承问题(菱形)。

然而,只有一个 getter 或 setter 而不进行任何实现应该是可行的。

public interface I { function get A():int; }

(我手头没有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.

public interface I { function get A():int; }

(I don't have the AS3 compiler at hand)

独孤求败 2024-08-05 22:18:56

我不是一个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.

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