flex:引用类变量

发布于 2024-11-04 21:21:04 字数 345 浏览 4 评论 0原文

我的类中有很多变量。在某些情况下,我想根据明确定义的逻辑将 then 设置为 null/“temp”等。挑战在于列出多个位置的变量——繁琐且容易出错。

classname.speed=NaN

classname.speedtype="not_set"

classname.distance=NaN

classname.distancetype="not_set"

理想情况下,更喜欢一种以编程方式引用这些变量并设置类似的方法 “对于所有类变量 - 如果变量以类型结尾,则设置为“not_set”;对于设置为 NaN 的其他变量,

我怎样才能实现这一点?任何指针都会有所帮助

I have a bunch of variables in a class. There are situations when I want to set then to null/ "temp" etc as per a well defined logic. The challenge is to list out the variables at multiple places- tedious and error-prone.

classname.speed=NaN

classname.speedtype="not_set"

classname.distance=NaN

classname.distancetype="not_set"

Ideally, would prefer a way to refer to these variables programatically and set something like
"for all class variables- if variable ends in type, set as "not_set"; for other variables set as NaN

How can I achieve this? Any pointers will help

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

鹊巢 2024-11-11 21:21:04

最简单的方法就是编写函数来清除它们。
如果您想要更自动化的东西,则需要付出努力 - 查看 内省API。基本上,您在类上调用describeType,它会返回 XML 描述。所有变量以及其他信息都将在那里列出。然后,您可以解析返回的 XML 并将所有变量设置为所需的值,并使用方括号语法动态访问它们:

var myClass:MyClass = new MyClass();
myClass["varName"] = "new value";

The simplest approach would be just write function to clear them all.
If you want something more automatic, it will requre efforts - look at introspection api. Basically, you call describeType on your class and it returns XML description. All variables will be listed there, along with other info. Then you can parse returned XML and set all variables to needed value, accessing them dynamically with square bracket syntax:

var myClass:MyClass = new MyClass();
myClass["varName"] = "new value";
陌若浮生 2024-11-11 21:21:04

它可以通过继承来实现,即实现接口扩展
其中包含公共字段

public class MyClass
{
    public a:String = null;
    public b:String = null;

    public function MyClass()
    {
    }

}

,其中包含公共 var 和子类可能是

public class MyClassChild extends MyClass
{
    public var c:String = null;
    public function MyClassChild()
    {
        super();
        this.a ="";
        this.b ="";
    }

}

,您可以强制转换或使用每个循环来设置值

var temp:MyClassChild  = new MyClassChild ();

MyClass(temp).a = "Hello World";

希望有帮助

It can be achieved through Inheritance i.e. implementing interface or extending class
which contains common fields

public class MyClass
{
    public a:String = null;
    public b:String = null;

    public function MyClass()
    {
    }

}

which contains common var and Child Class could be

public class MyClassChild extends MyClass
{
    public var c:String = null;
    public function MyClassChild()
    {
        super();
        this.a ="";
        this.b ="";
    }

}

and you can cast or use for each loop to set values

var temp:MyClassChild  = new MyClassChild ();

MyClass(temp).a = "Hello World";

Hopes that helps

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