2011-06-03 58 views
5

我有一個類這在目前是相當混亂/重複:創建多個只讀屬性動態地在AS3

public class AvFramework extends Object 
{ 
    // vars 
    private var _handler:AvHandler; 
    private var _keyboard:AvKeyboard; 
    private var _manager:AvManager; 

    /** 
    * Constructor 
    */ 
    public function AvFramework() 
    { 
     _handler = new AvHandler(); 
     _keyboard = new AvKeyboard(); 
     _manager = new AvManager(); 

     // attach 
     _handler.framework = this; 
     _keyboard.framework = this; 
     _manager.framework = this; 
    } 

    /** 
    * Getters 
    */ 
    public function get keyboard():AvKeyboard{ return _keyboard; } 
    public function get manager():AvManager{ return _manager; } 
} 

該類只將需要利用越來越多班,和我真的不想爲這個像上面的3個巨大的名單。

有沒有辦法做到上述動態 - 可能在字符串循環中使用getDefinitonByName()來表示我想要創建的類。

我還希望屬性爲只讀,並通過framework.myDynamicVarHere訪問。

我沿着這些路線思考的東西:

  1. 我創造我想要創建的情況下,與他們應當由被訪問的變量名稱配對的類的列表。
  2. 我需要通過this["var"] = new Blah();

的快速片段使課堂dynamic,這樣我可以設置增值經銷商那裏我的想法會:

var required:Object = 
{ 
    keyboard: "avian.framework.background.AvKeyboard", 
    manager: "avian.framework.background.AvManager", 
    handler: "avian.framework.background.AvHandler" 
}; 

var i:String; 
for(i in required) 
{ 
    var Req:Class = Class(getDefinitionByName(required[i])); 

    this[i] = new Req(); 
    AvFrameworkObject(this[i]).framework = this; 
} 

只是不知道如何我將能夠使這些只讀。

+0

不錯的問題! – Eugeny89 2011-06-03 10:08:53

+0

this:'var i:String; for(i in required)'是沒有必要的。用'for(var i:String in required)',你只需要初始化字符串一次,字符串在for循環之後仍然存在。額外的產品線唯一得到您的是...另外一條線。 – cwallenpoole 2011-06-03 11:19:41

+0

@cwallenpoole的確,這只是對我更好。 @ Eugeny89謝謝:) – Marty 2011-06-04 04:07:45

回答

4

您可以使用Proxy類來控制get/set調用並完成Eugeny89答案。 看http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/utils/Proxy.htmlhttp://blog.joa-ebert.com/2007/05/25/as3-proxy-example/

var required:Object = 
{ 
    keyboard: "avian.framework.background.AvKeyboard", 
    manager: "avian.framework.background.AvManager", 
    handler: "avian.framework.background.AvHandler" 
}; 
... 

import flash.utils.Proxy; 
import flash.utils.flash_proxy; 
public dynamic class AvFramework extends Proxy { 
    private var holder: Object = {}; 

    public function AvFramework() { 
     var i:String; 
     for(i in required) 
     { 
      var Req:Class = Class(getDefinitionByName(required[i])); 
      var name = //get Class name from required[i] e.g. AvKeyboard from avian.framework.background.AvKeyboard 
      holder[name] = new Req(); 
      holder[name].framework = this; 
     } 
    } 

    flash_proxy override function getProperty(name: *): * 
    { 
     return holder[name]; 
    } 

    flash_proxy function setProperty(name:*, value:*):void 
    { 
     // Do nothing 
    } 
} 

有了這個代碼,當你做myAvFrameworkObj.something,的getProperty()被調用automaticaly,你會得到從「名」變量屬性名稱。

+0

你應該有private var holder:Object = {};(並且不會在構造函數中初始化它新的Object)如果你想遵循Adobe文體標準 – cwallenpoole 2011-06-03 11:18:06

+0

爲什麼不能,謝謝 – 2011-06-03 11:53:46

+0

偉大的答案+1 +1! – Eugeny89 2011-06-03 13:05:24

1

也許你會試着來包裝對象classws那長長的名單:

var required:Object = 
{ 
    keyboard: "avian.framework.background.AvKeyboard", 
    manager: "avian.framework.background.AvManager", 
    handler: "avian.framework.background.AvHandler" 
}; 
... 

public class AvFramework extends Object { 
    private var holder: Object; 

    public function AvFramework() { 
     holder = new Object(); 

     var i:String; 
     for(i in required) 
     { 
      var Req:Class = Class(getDefinitionByName(required[i])); 
      var name = //get Class name from required[i] e.g. AvKeyboard from avian.framework.background.AvKeyboard 
      holder[name] = new Req(); 
      holder[name].framework = this; 
     } 
    } 

    public var getHolder() { return holder; } 
} 

你可以參考如AvKeyboardframework.getHolder()["AvKeyboard"]或甚至由framework.getHolder()[someVar]。因此,從required開始的所有這些類都是隻讀的,因爲holder是隻讀的。

希望能幫到你!

+0

好的答案 - 我有同樣的想法,但是通過framework.things通過framework.getHolder()[「things」]訪問所有內容(從框架對象)是非常錯誤的: – Marty 2011-06-04 04:04:44