问题描述
我从未真正了解 AS3 的一件事是,您不能同时拥有私有 set 方法和公共 get 方法.
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);
例如:
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 关键字?
is there no way to use the set keyword on a private function?
推荐答案
MXML 编译器不支持具有混合作用域/命名空间的 getter 和 setter.有几张票就此打开:
The MXML compiler does not support getters and setters with mixed scopes/namespaces. There are a few tickets open regarding this:
- https://bugs.adobe.com/jira/browse/ASL-44
- https://bugs.adobe.com/jira/browse/SDK-25646
- https://bugs.adobe.com/jira/browse/ASL-112.
这很烦人,但至少 Adobe 意识到了这一点.有一种方法可以通过使用自定义命名空间和对 getter 或 setter 的完全限定引用来实现混合命名空间 getter 和 setter.
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.
package {
use namespace my_namespace
public class MyClass {
private var _name:String;
public function get name():String {
return _name;
}
my_namespace function set name(value:String):void {
_name = value;
}
}
public class MySubClass extends MyClass {
public function MySubClass(name:String) {
super.my_namespace::name = name;
}
}
}
}
这篇关于ActionScript - 只读属性和私有集方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!