2011-04-04 37 views
3

我有拥有一些常数和将收到对象文本(关联阵列)配有一些像这样的数据的类:对象常量的常量作为键(在密钥值对)

var ConfigObj:Config = new Config({ 
    "Some" : 10, 
    "Other" : 3, 
    "Another" : 5 
}); 

类看起来是这样的:

public dynamic class Config 
{ 
    static public const SomeProperty:String = "Some"; 
    static public const OtherProperty:String = "OtherProperty"; 
    static public const AnotherProperty:String = "AnotherProperty"; 

    public function Config(settings:Object) 
    { 
     // Doing some stuff here 
    } 
} 

的问题是,我怎么能传递常量,这样的:

var ConfigObj:Config = new Config({ 
    Config.SomeProperty : 10, 
    Config.OtherProperty : 3, 
    Config.AnotherProperty : 5 
}); 

此外,如果可能,我想保持内联。

var MyObj:MyClass = new MyClass({x:1, y:2, z:3}); 

是,对我来说,远不如:

var Temp:Object = new Object(); 
Temp.x = 1; // Or Temp[x] = 1; 
Temp.y = 2; // Or Temp[y] = 2; 
Temp.z = 3; // Or Temp[z] = 3; 

var MyObj:MyClass = new MyClass(Temp); 
+0

其实你的第一个例子将编译没有双引号。它们是可选的(只要密钥遵循标识符命名规则,就像你的那样)。不幸的是,{}语法不支持这种“替代”。 – 2011-04-04 21:02:27

+0

我不确定,但我想我明白你想要做什么(有点)。你可能想看看Flash的Dictionary类;它可能会提供您正在寻找的键值对。 – 2011-04-04 22:13:56

回答

2

我开始觉得你是过于复杂的配置对象的感觉,但是如果你想使用常量设置键 - 值对,你需要使用一个临时变量:

var o:Object, configObj:Config; 
o = {}; 
o[Config.SomeProperty] = 'foo'; 
o[Config.OtherProperty] = 'bar'; 
o[Config.AnotherProperty] = 'baz'; 

configObj = new Config(o); 

要问的一个重要问题是:这些属性真的是恒定的吗?如果是这样,那么就在使用字符串字面量小的风险,当你实例化对象:

new Config({ 'SomeProperty':'foo', 'OtherProperty':'bar', 'AnotherProperty':'baz' }); 

当然,这不是灵活,如果在常量的值的变化。

+0

嗯,我使用常量只是因为我需要保持代码的完整性......我不能单纯依赖字符串,我需要编译时检查,并使用常量来确保。还有一件事是,我希望它是内联的,创建另一个对象来保持临时数据不是一个好的方法。 – NemoStein 2011-04-04 20:53:10

+0

@NemoStein,那你为什么不使用参数?或者,为什么不能直接在Config对象上设置属性,然后删除对占位符对象的需求。希望所有内联都被高估并且不易读。 – zzzzBov 2011-04-04 21:02:21

+0

我同意*“被高估并且难以理解”* ...但是,我的老板不会!关于使用参数,它应该按照特定的顺序进行,但我的配置类使用了一长串参数,用户不应该传递每一个参数,甚至不需要进行排序......并将其直接设置在Config对象上会打破“内联“风格...... – NemoStein 2011-04-04 21:06:30

0

如果你想强制进行类型检查和参数命名,你不应该使用dynamic class或通过Object,但你应该编码一个Config class所有可能的可用。 在设置参数表时返回Config class将允许您内联该呼叫。 它需要更多的作品,但它更安全恕我直言。

例:

class Config { 
    protected var _somePropertySet:Boolean 
    public function get isSomePropertySet():Boolean{ 
     return _somePropertySet 
    } 
    protected var _someProperty:String; 
    public function setSomeProperty(value:String):Config{ 
     _somePropertySet=true 
     _someProperty = value 
     return this 
    } 
    public function get someProperty():String{ 
     return _someProperty 
    } 

    protected var _someOtherPropertySet:Boolean 
    public function get isSomeOtherPropertySet():Boolean{ 
     return _someOtherPropertySet 
    } 
    protected var _someOtherProperty:int; 
    public function setSomeOtherProperty(value:int):Config{ 
     _someOtherPropertySet=true 
     _someOtherProperty = value 
     return this 
    } 

    protected var _someAnotherPropertySet:Boolean 
    public function get isSomeAnotherPropertySet():Boolean{ 
     return _someAnotherPropertySet 
    } 
    protected var _someAnotherProperty:Object; 
    public function setSomeAnotherProperty(value:Object):Config{ 
     _someAnotherPropertySet=true 
     _someAnotherProperty = value 
     return this 
    } 
} 

class Tmp { 
    public function Tmp(config:Config) { 
     initFromConfig(config) 
    } 

    protected function initFromConfig(config:Config):void { 
     if (config.isSomePropertySet){ 
      //.. 
     } 
     if (config.isSomeOtherPropertySet){ 
      //.. 
     } 
     if (config.isSomeAnotherPropertySet){ 
      //.. 
     } 
    } 
} 
var t:Tmp=new Tmp(new Config().setSomeProperty("foo").setSomeOtherProperty(5).setSomeAnotherProperty(null))