简体   繁体   English

AS3使用字符串定位控制器类变量

[英]AS3 targeting controller class variable using string

I'm looking for a way of condensing some of my AS3 code to avoid almost duplicate commands. 我正在寻找一种方法来压缩我的一些AS3代码,以避免几乎重复的命令。

The issue is that I have multiple variables with almost the same name eg frenchLanguage, englishLanguage, germanLanguage, spanishLanguage 问题是我有几个名字几乎相同的变量,例如frenchLanguage,englishLanguage,germanLanguage,spanishLanguage

My Controller class contains public static variables (these are accessed across multiple classes) and I need a way to be able to call a few of these variables dynamically. 我的Controller类包含公共静态变量(这些变量可以在多个类中访问),我需要一种能够动态调用其中一些变量的方法。 If the variables are in the class you are calling them from you can do this to access them dynamically: 如果变量在您调用它们的类中,则可以执行此操作以动态访问它们:

this["spanish"+"Language"]

In AS3 it's not possible to write something like: 在AS3中,不可能写出如下内容:

Controller.this["spanish"+"Language"]

Is there any way to achieve this? 有没有办法实现这个目标? Although everything is working I want to be able to keep my code as minimal as possible. 虽然一切正常,但我希望能够尽可能减少代码。

It is possible to access public static properties of a class this way (assuming the class name is Controller as in your example: 可以通过这种方式访问​​类的public static属性(假设类名是Controller如示例所示:

Controller['propertyName']

I'm not sure how this helps to have "minimal code", but this would be a different topic/question, which might need some more details on what you want to achive. 我不确定这对于“最小代码”有什么帮助,但这将是一个不同的主题/问题,可能需要更多关于你想要实现的细节。

Having said that, I like the approach DodgerThud suggests in the comments of grouping similar values in a ( dynamic ) Object or Dictonary and give it a proper name. 话虽如此,我喜欢DodgerThud在( dynamicObjectDictonary中对类似值进行分组的评论中所建议的方法,并给它一个正确的名称。

Keep in mind, that if the string you pass in as the key to the class or dynamic object is created from (textual) user input you should have some checks for the validity of that data, otherwise your programm might crash or expose other fields to the user. 请记住,如果作为类或动态对象的键传入的字符串是从(文本)用户输入创建的,则应检查该数据的有效性,否则您的程序可能会崩溃或将其他字段暴露给用户。

It would make sense to utilize a Dictionary object for a set of variables inherited: it provides a solid logic and it happens to work... 将一个Dictionary对象用于一组继承的变量是有意义的:它提供了一个可靠的逻辑,它恰好工作......

I do not think this is what you are trying to accomplish. 我不认为这是你想要完成的。 I may be wrong. 我可能错了。

Classes in AS3 are always wrapped within a package - this is true whether you have compiled from Flash, Flex, Air, or any other... AS3中的类总是包装在一个package - 无论您是从Flash,Flex,Air还是其他任何程序编译都是如此......

Don't let Adobe confuse you. 不要让Adobe迷惑你。 This was only done in AS3 to use Java-Based conventions. 这只在AS3中完成,以使用基于Java的约定。 Regardless, a loosely typed language is often misunderstood, unfortunately. 无论如何,遗憾的是,松散类型的语言经常被误解。 So: 所以:

this["SuperObject"]["SubObject"]["ObjectsMethod"][ObjectsMethodsVariable"](args..);

... is technically reliable because the compiler avoids dot notation but at runtime it will collect a lot of unnecessary data to maintain those types of calls. ...技术上可靠,因为编译器避免使用点符号,但在运行时它会收集大量不必要的数据来维护这些类型的调用。

If efficiency becomes an issue.. 如果效率成为一个问题..

Use: 采用:

package packages {
import flash.*.*:
class This implements ISpecialInterface {
// Data Objects and Function Model
// for This Class 
}
package packages {
import...
class ISpecialInterface extends IEventDispatcher

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM