我有一个类这在目前是相当混乱/重复:
I have a class which at the moment is quite messy/repetitive:
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对这个庞大的名单像上面。
This class is only going to need to make use of more and more classes, and I don't really want to have 3 huge lists for this like above.
有没有一种方法可以动态做到以上 - 也许用 getDefinitonByName()
在字符串中重新present我想创建的类的环
Is there a way to do the above dynamically - maybe using getDefinitonByName()
in a loop of strings to represent the classes I want to create.
我也想要的属性进行只读,并可以通过 framework.myDynamicVarHere
访问。
I also want the properties to be read-only and to be accessed via framework.myDynamicVarHere
.
我在想的东西沿着这些路线:
I'm thinking something along these lines:
在我创造我想要创建实例的类的列表,搭配它们应该被访问的变量名称。 在我需要让类动态
,这样我可以通过设置瓦尔这种[变种] =新布拉赫();
I create a list of the classes I want to create instances of, paired with the variable name they should be accessed by.
I will need to make the class dynamic
so that I can set the vars via this["var"] = new Blah();
在我的想法是要快速片段:
Quick snippet of where my thoughts are going:
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;
}
只是不知道如何我将能够使这些只读的。
Just not sure how I would be able to make these read-only.
您可以使用Proxy类来控制的get / set电话,完整Eugeny89答案。 看的http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/utils/Proxy.html或 http://blog.joa-ebert.com/2007 / 05/25 / AS3代理 - 例如/
You can use Proxy class to control get/set call and complete Eugeny89 answer. Look at http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/utils/Proxy.html or http://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
}
}
通过这个code,当你做myAvFrameworkObj.something,的getProperty()是automaticaly打电话,你获得的属性名称名的变量。
With this code, when you do myAvFrameworkObj.something, getProperty() is automaticaly call and you get property name from "name" variable.