flex:引用类变量
我的类中有很多变量。在某些情况下,我想根据明确定义的逻辑将 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
最简单的方法就是编写函数来清除它们。
如果您想要更自动化的东西,则需要付出努力 - 查看 内省API。基本上,您在类上调用
describeType
,它会返回 XML 描述。所有变量以及其他信息都将在那里列出。然后,您可以解析返回的 XML 并将所有变量设置为所需的值,并使用方括号语法动态访问它们: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 和子类可能是
,您可以强制转换或使用每个循环来设置值
希望有帮助
It can be achieved through Inheritance i.e. implementing interface or extending class
which contains common fields
which contains common var and Child Class could be
and you can cast or use for each loop to set values
Hopes that helps