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);
4

2 回答 2

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' } );

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

于 2011-04-04T20:47:40.210 回答
0

如果要强制执行类型检查和参数命名,则不应使用 adynamic class或传递 a Object,但应Config class使用所有可用的可能性对 a 进行编码。返回Config classwhile 设置参数将允许您内联调用。它需要更多的工作,但它更安全恕我直言。

前任:

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))
于 2011-04-04T21:48:09.240 回答