开发者

Best practice for interface with a getter function that could return multiple types

开发者 https://www.devze.com 2023-01-11 18:54 出处:网络
I have several data manipulation widgets that all implement a custom IPropertyEditor interface. I would like to include a GetValue getter, but the return types could be String, Boolean or in开发者_运维

I have several data manipulation widgets that all implement a custom IPropertyEditor interface. I would like to include a GetValue getter, but the return types could be String, Boolean or in开发者_运维问答t, off the top of my head. In AS3, all of that inherits from Object, so I could have GetValue return an object, but I don't feel great about that approach. At the risk of asking a subjective question, does anyone have any recommendations on how to approach this?


In ActionScript I'm pretty sure you can set a variable return type by defining a function in the following way:

public function getValue():* {
    return "Any of these would be fine.";
    return true;
    return 1;
    return new Sprite();
}

Hope that helps.


In practice, there is an actual getter/setter model in ActionScript. For your case, you could use it like this:

private var _value:*;

public function get value() : * {
  return _value;
}

public function set value(val:*) : void {
  if (typeof val == "int" || typeof val == "boolean" || typeof val == "string") {
    _value = val;
  }
}

This limits the user to setting (per your requirements) the value of this "value" property to data types int, Boolean, or String.

0

精彩评论

暂无评论...
验证码 换一张
取 消