ActionScript - 只读属性和私有设置方法?
我从未真正理解 AS3 的一件事是,不能同时使用私有 set 方法和公共 get 方法。
在我的班级中,我想分配调用私有集函数的值:
myNumber = 22
;
但我需要将该数字作为参数传递给函数,
myNumber(22);
例如:
package
{
//Imports
import flash.display.Sprite
//Class
public class NumberClass extends Sprite
{
//Properties
private var myNumberProperty:Number
//Constructor
public function NumberClass(myNumber:Number):void
{
this.myNumber = myNumber;
init();
}
//Initialize
private function init():void
{
trace(myNumber);
}
//My Number Setter
private function set myNumber(value:Number):void
{
myNumberProperty = Math.max(0, Math.min(value, 100));
}
//My Number Getter
public function get myNumber():Number
{
return myNumberProperty;
}
}
}
是否无法在私有函数上使用 set 关键字?
one thing i've never really understood about AS3 is that you can't have a private set method and a public get method together.
from within my class i would like to assign values that would call a private set function:
myNumber = 22
;
but i need to pass that number as a parameter to a function
myNumber(22);
for example:
package
{
//Imports
import flash.display.Sprite
//Class
public class NumberClass extends Sprite
{
//Properties
private var myNumberProperty:Number
//Constructor
public function NumberClass(myNumber:Number):void
{
this.myNumber = myNumber;
init();
}
//Initialize
private function init():void
{
trace(myNumber);
}
//My Number Setter
private function set myNumber(value:Number):void
{
myNumberProperty = Math.max(0, Math.min(value, 100));
}
//My Number Getter
public function get myNumber():Number
{
return myNumberProperty;
}
}
}
is there no way to use the set keyword on a private function?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
MXML 编译器不支持具有混合作用域/命名空间的 getter 和 setter。有一些与此相关的票证:
这很烦人,但至少 Adobe 意识到了这一点。有一种方法可以通过使用自定义命名空间和对 getter 或 setter 的完全限定引用来实现混合命名空间 getter 和 setter。
The MXML compiler does not support getters and setters with mixed scopes/namespaces. There are a few tickets open regarding this:
It's quite annoying, but at least Adobe is aware of it. There is a way to accomplish mixed namespace getters and setters by using custom namespaces and fully-qualifying references to the getter or setter.
有什么理由
不行吗?它给出了什么错误?我一直在 Flex 中这样做,所以我不确定它是否只在那里工作......但我不这么认为。
编辑:看起来这是一个编译器错误。这是一篇博客文章,其中包含解决方案 http://blogagic。 com/230/struggling-with-flex-error-1000-ambiguously-reference-to
Is there a reason
doesn't work? What error does it give? I've done this all the time in Flex, so I'm not sure if it only works there... I wouldn't think so, though.
edit: Looks like this is a compiler bug. Here's a blog post with the solution http://blogagic.com/230/struggling-with-flex-error-1000-ambiguous-reference-to
不太理想,但是您可以创建一个私有方法吗?
Not ideal, but could you just create a private method?
不,如果一个是私有的,那么它们都必须是私有的。无论其他人如何认为这都不是一个错误。
setter/getter 背后的想法是将代码与公共隔离。
记住这个 OOP
您还应该尝试遵循带有前导 _
的 var 名称的典型约定
参考
所谓的错误报告在这里
Nope If one is private they both have to be private. Regardless of what others think this is not a bug.
The idea behind setters/getters is to isolate code from the public.
Remember this OOP
You should also try to stay with typical convention for the var name with a leading _
refer
The so called bug report is here