开发者

synthesize getters/setters in AS3

开发者 https://www.devze.com 2022-12-11 08:10 出处:网络
Is开发者_StackOverflow there a way to synthesize getters/setters in AS3? It\'s very common that you have a class with lots of variables, especially in math calculations (Model in MVC pattern), that yo

Is开发者_StackOverflow there a way to synthesize getters/setters in AS3? It's very common that you have a class with lots of variables, especially in math calculations (Model in MVC pattern), that you'd like to expose. Is there something like synthesize property in Objective-C, that allows to generate getters/setters?

Thanks,

Nava


You can try regex. Take a backup before attempting this.

For example, if your private variables are named _str and you want it's public getter/setter to be named str, you can use the following patterns. Hit Ctrl-F in flex builder (or eclipse), tick the regex check box, and add the following patterns to the search and replace input fields respectively. Now hit 'Find' to find the property declaration and hit 'Replace' to generate setter and getter.

^((\t)+)private\s+var\s+_(\w+):(\w+)\s*;\s*(\n)

$0$5$1public function set $3(value:$4):void$5$1{$5$1$2_$3 = value;$5$1}$5$5$1public function get $3():$4$5$1{$5$1$2return _$3;$5$1}$5$5

This pattern was tested on

        private var _str:String;//indented by two tabs

And it successfully generated:

    private var _str:String;

    public function set str(value:String):void
    {
        _str = value;
    }
    public function get str():String
    {
        return _str;
    }


For those that are using the Flex Builder 3 IDE, there are plug-ins for creating and placing code snippets, e.g. here: http://www.insideria.com/2008/04/flex-builder-enhancements-snippets-and-todo.html (there are some other small but useful tools compiled on that site)

These can be used for a more or less comfortable getter/setter generation.

However I only create explicit getters and setters if I need to place some extra code there. There are some who say it breaks encapsulation if you don't use getters and setters. However, with a language where you can add getters and setters later without changing the interface, I don't think that this is true anymore. To the user, it is completely transparent if he is using a plain variable assign or a function set varName(arg:object):void.

And also bear in mind, that if you make a variable [Bindable] (i.e. without an explicit event name), the compiler creates getters and setters for your variable wihtout you even noticing it (except there are already getters and setters of course). This is BTW a quick&dirty way to implement Interfaces that declare getter and setter functions


If you just make it a public property you don't have to worry about getters and setters:

public var str:String;

However, if you need anything along the lines of event dispatching to notify yourself of property changes, you'll need to code it yourself. Hopefully whatever IDE you're using may help by generating this kind of code for you, or you may need to set up some kind of code snippet the DIE can inject for you.

Avoid copy and paste if you can, you're bound to miss changing the name of something that will cause you issues down the road.


EDIT: If you want to use the IDE (eclipse) . You can go rightClick on class->Source->Generate Getters and Setters

It is not the same use as the synthesize, but if you want just to expose them and don't have to use the getMethod() notation, it can help you declaring the methods with the keywords get/set like this.

public function get example() : int
{
             return example;
}
public function set example( value : String ) : void
{
             example = value;
}

When you use it the time you access you can use the variables as if they were public and the methods will be called instead of direct access

0

精彩评论

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