当 getter/setter 具有不同的可见性时,引用不明确
以下代码在编译时引发对 value
的不明确引用:
import flash.display.Sprite;
public class Main extends Sprite
{
private var _value : Number = 0.;
public function get value() : Number { return _value; }
private function set value(v : Number) : void { _value = v; }
public function Main() : void
{
value = 42.;
}
}
我怀疑编译器中存在某种错误,尽管我实际上没有阅读 ECMA 标准。
在有人问这些问题之前:
- 私人设置者确实有道理。
- 自定义命名空间也存在歧义(这是我面临的问题)。
The following code raises an ambiguous reference to value
at compile time:
import flash.display.Sprite;
public class Main extends Sprite
{
private var _value : Number = 0.;
public function get value() : Number { return _value; }
private function set value(v : Number) : void { _value = v; }
public function Main() : void
{
value = 42.;
}
}
I suspect some kind of bug in the compiler, though I didn't actually read the ECMA standard.
Before someone asks those questions:
- Private setters do make sense.
- The ambiguity also exists with custom namespaces (which is the problem I'm facing).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这确实是编译器中的一个错误,并且在错误中列出了 。它表示这是开发商的过度现场,不会很快得到修复。
如果您需要专门运行一个函数来进行私有设置(而不是仅仅分配值,在这种情况下您可以完全省略 setter 函数并且它会运行),那么您将必须像 Sandro 所说的那样运行一个单独的函数。
it is indeed a bug in the compiler, and it is listed in the bugs. its stated that its an oversite of the developers and wont be fixed any time soon.
if you are needing to specifically run a function to set privately (rather than just assign the value, in which case you can leave out the setter function completely and itll run) then you will have to run a seperate function as Sandro said.
我想这可能是AS3的限制。您可以创建一个名为 setValue() 的私有函数,或者如果您设置了 setter,您可能可以摆脱这个问题,尽管它不是很漂亮。
I think this may be a limitation of AS3. You could create a private function called setValue() or if your set on having a setter you might be able to get away with this, although it's not very pretty.