ActionScript - 确定值是否为类常量

发布于 2024-10-21 18:57:19 字数 915 浏览 1 评论 0原文

如果某个特定函数在没有传递值的情况下无法工作,并且该值恰好是包含该函数的类的公共常量,我想抛出参数错误。

无论如何,有没有办法确定一个类是否拥有一个公共常量,而不必遍历所有常量?

像这样的东西:

public static const HALIFAX:String = "halifax";
public static const MONTREAL:String = "montreal";
public static const TORONTO:String = "toronto";

private var cityProperty:String;

public function set city(value:String):void
     {
     if (!this.hasConstant(value))
        throw new ArgumentError("set city value is not applicable.");

     cityProperty = value;
     }

public function get city():Strig
     {
     return cityProperty;
     }

目前,对于此功能,我必须像这样编写城市设置器函数:

public function set city(value:String):void
     {
     if (value != HALIFAX && value != MONTREAL && value != TORONTO)
        throw new ArgumentError("set city value is not applicable.");

     cityProperty = value;
     }

这是完成此任务的唯一方法吗?

i'd like to throw an argument error if a particular function doesn't work without a passed value that also happens to be a public constant of the class containing the function.

is there anyway to determine if a class owns a public constant instead of having to iterate thru all of them?

something like this:

public static const HALIFAX:String = "halifax";
public static const MONTREAL:String = "montreal";
public static const TORONTO:String = "toronto";

private var cityProperty:String;

public function set city(value:String):void
     {
     if (!this.hasConstant(value))
        throw new ArgumentError("set city value is not applicable.");

     cityProperty = value;
     }

public function get city():Strig
     {
     return cityProperty;
     }

currently, for this functionality i have to write the city setter function like this:

public function set city(value:String):void
     {
     if (value != HALIFAX && value != MONTREAL && value != TORONTO)
        throw new ArgumentError("set city value is not applicable.");

     cityProperty = value;
     }

is this the only way to accomplish this task?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

嗼ふ静 2024-10-28 18:57:19

是的,如果您使用反射:

private var type:Class;
private var description:XML;

private function hasConstant (str : String ) : Boolean 
{
    if (description == null) 
    {
        type = getDefinitionByName (getQualifiedClassName (this)) as Class;
        description = describeType (type);      
    }
    for each ( var constant:XML in description.constant) 
    {
        if (type[constant.@name] == str) return true;
    }
    return false;
}

请注意,要使其正常工作,所有常量必须始终是声明为 public static const 的 String 对象。

Yes, if you use reflections:

private var type:Class;
private var description:XML;

private function hasConstant (str : String ) : Boolean 
{
    if (description == null) 
    {
        type = getDefinitionByName (getQualifiedClassName (this)) as Class;
        description = describeType (type);      
    }
    for each ( var constant:XML in description.constant) 
    {
        if (type[constant.@name] == str) return true;
    }
    return false;
}

Note that for this to work, all constants must always be String objects declared public static const.

药祭#氼 2024-10-28 18:57:19

我自己正在寻找这个问题的答案,并发现 hasOwnProperty() 不适用于静态属性,这很烦人。但事实证明,如果将类强制转换为 Class 对象,它确实可以工作。

这是一个例子:

public final class DisplayMode
{
  public static const one:   String = "one";
  public static const two:   String = "two";
  public static const three: String = "three";

  public static function isValid(aDisplayMode: String): Boolean {
    return Class(DisplayMode).hasOwnProperty(aDisplayMode);
  }
}

我将此解决方案归功于此讨论中的 jimmy5804,所以向他致敬。

I was looking for an answer to this question myself and found it annoying that hasOwnProperty() did not work for static properties. Turns out though, that if you cast your class to a Class object, it does work.

Here's an example:

public final class DisplayMode
{
  public static const one:   String = "one";
  public static const two:   String = "two";
  public static const three: String = "three";

  public static function isValid(aDisplayMode: String): Boolean {
    return Class(DisplayMode).hasOwnProperty(aDisplayMode);
  }
}

I owe this solution to jimmy5804 from this discussion, so hats off to him.

楠木可依 2024-10-28 18:57:19

您应该能够使用括号表示法来执行此操作。例如:

var foo:Sprite = new Sprite();
foo.rotation = 20;
trace( foo["x"], foo["rotation"]); // traces "0 20"

或者更具体地针对您的情况:

var bar:String = "rotation";
trace( foo[bar] ); // traces "20"

您在这里唯一需要注意的是,如果您请求不存在的对象属性,括号访问器将抛出 ReferenceError ,例如:

trace ( foo["cat"] ); // throws ReferenceError

但它不会如果您要求静态属性,则抛出:

trace ( Sprite["cat"] ); // traces "undefined"

所以在您的情况下您可以尝试:

if ( this[value] == undefined ) {
    throw new ArgumentError("set city value is not applicable.");
}

编辑:
抱歉,我将 const 的名称与其值混淆了。

为了解决您的问题,您必须使 String 值与 const 的名称相同,例如:

public static const HALIFAX:String = "HALIFAX";

然后您可以使用如上所述的查询,它会给您所需的结果。

You should be able to use bracket notation to do this. For example:

var foo:Sprite = new Sprite();
foo.rotation = 20;
trace( foo["x"], foo["rotation"]); // traces "0 20"

or more specific to your case:

var bar:String = "rotation";
trace( foo[bar] ); // traces "20"

The only thing you have to look out for here, is that the bracket accessor will throw a ReferenceError if you ask for an object property that isn't there, such as:

trace ( foo["cat"] ); // throws ReferenceError

But it will not throw if you are asking for a static property:

trace ( Sprite["cat"] ); // traces "undefined"

So in your case you might try:

if ( this[value] == undefined ) {
    throw new ArgumentError("set city value is not applicable.");
}

EDIT:
Sorry, I was confusing the const's names with their values.

For this to work on your problem you would have to make the String value the same as the const's name, so for example:

public static const HALIFAX:String = "HALIFAX";

then you could use the query as described above and it would give you the desired result.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文