Actionscript, can a class be accessed using a variable name?

时间:2017-06-15 09:36:37

标签: oop actionscript-3 actionscript

I wish to access many classes and variables, I would like to do this by dynamically setting the class name and variable name. Currently I am using

MyClass["myVariable1"]

to dynamically access the variable name

MyClass.myVariable1

I want to also dynanmically acces the class name, something like

["MyClass"]["myVariable1"]

But this does not work.

The purpose is that I have shared object with many user settings, I want to iterate through the shared object and set all the user settings across all the classes. I think if I cant dynamically access the class I must have a statement for each and every class name/variable.

2 个答案:

答案 0 :(得分:3)

我建议反对这种做法。虽然技术上可行,但就像欢迎应用程序架构中的灾难一样:

  1. 你依赖于你无法控制的东西:在Flash命名类的路上。

  2. 您将使用标识符重命名模糊处理来保护您的代码,因为它会使您的代码无效。

  3. 编译时错误检查优于运行时,您将把它留给运行时。如果在非调试环境中发生故障,您将永远不会知道。

  4. 下一个使用您的代码的开发人员(可能是您在几年内)将很难找到初始数据的来源。

  5. 所以,有了上述所有内容,我建议您切换到另一个模型:

    options(stringsAsFactors = FALSE)
    library(plyr)
    library(dplyr)
    library(xml2)
    
    uuu_df <- data.frame(x = c('http://www.magicbricks.com/property-for-sale/residential-real-estate?bedroom=1&proptype=Multistorey-Apartment,Builder-Floor-Apartment,Penthouse,Studio-Apartment&cityName=Thane&BudgetMin=5-Lacs&BudgetMax=5-Lacs',
                               'http://www.magicbricks.com/property-for-sale/residential-real-estate?bedroom=2&proptype=Multistorey-Apartment,Builder-Floor-Apartment,Penthouse,Studio-Apartment&cityName=Thane&BudgetMin=5-Lacs&BudgetMax=5-Lacs',
                               'http://www.magicbricks.com/property-for-sale/residential-real-estate?bedroom=3&proptype=Multistorey-Apartment,Builder-Floor-Apartment,Penthouse,Studio-Apartment&cityName=Thane&BudgetMin=5-Lacs&BudgetMax=90-Lacs'))
    
    urlList <- llply(uuu_df[,1], function(url){     
    
      this_pg <- read_html(url)
    
      results_count <- this_pg %>% 
        xml_find_first(".//span[@id='resultCount']") %>% 
        xml_text() %>%
        as.integer()
    
      if(results_count > 0){
    
        cards <- this_pg %>% 
          xml_find_all('//div[@class="SRCard"]')
    
        df <- ldply(cards, .fun=function(x){
          y <- data.frame(wine = x %>% xml_find_first('.//span[@class="agentNameh"]') %>% xml_text(),
                          excerpt = x %>% xml_find_first('.//div[@class="postedOn"]') %>% xml_text(),
                          locality = x %>% xml_find_first('.//span[@class="localityFirst"]') %>% xml_text(),
                          society = x %>% xml_find_first('.//div[@class="labValu"]') %>% xml_text() %>% gsub('\\n', '', .))
          return(y)
        })
    
      } else {
        df <- NULL
      }
    
      return(df)   
    }, .progress = 'text')
    names(urlList) <- uuu_df[,1]
    
    bind_rows(urlList)
    

    在主要课程中你打电话

    package
    {
        import flash.net.SharedObject;
    
        public class SharedData
        {
            static private var SO:SharedObject;
    
            static public function init():void
            {
                SO = SharedObject.getLocal("my_precious_shared_data", "/");
            }
    
            static public function read(key:String):*
            {
                // if (!SO) init();
                return SO.data[key];
            }
    
            static public function write(key:String, value:*):void
            {
                // if (!SO) init();
                SO.data[key] = value;
                SO.flush();
            }
    
            // Returns stored data if any, or default value otherwise.
            // A good practice of default application values that might
            // change upon user activity, e.g. sound volume or level progress.
            static public function readSafe(key:String, defaultValue:*):*
            {
                // if (!SO) init();
                return SO.data.hasOwnProperty(key)? read(key): defaultValue;
            }
        }
    }
    

    然后,每个以这些数据为基础的类都应该有一个初始化块:

    SharedData.init();
    // So now your shared data are available.
    // If you are not sure you can call it before other classes will read
    // the shared data, just uncomment // if (!SO) init(); lines in SharedData methods.
    

    好吧,再说一次。上面的代码:

    • 不使用奇怪的做法且易于理解
    • 在每个班级中,任何人都可以清楚地看到数据来自哪里
    • 将在编译时检查错误
    • 可以进行模糊处理和保护

答案 1 :(得分:2)

您可以尝试将该类放入变量并从那里开始:

var myClass:Class = getDefinitionByName("MyClass") as Class;
myClass["myVariable1"] = x;