actionscript3中的getter和setter的另一种写法

actionscript3中的getter和setter的另一种写法
Another option is to use implicit getters and setters. Implicit getters and setters are declared as methods, but they look like properties. The syntax for a getter is as follows:

public function get name(  ):Datatype {
}

The syntax for a setter is as follows:

public function set name(value:Datatype):void {
}

这样在代码中调用counter.count=5时相当于调用了set count(5)方法。

Counter类如下:
public class Counter {
    private var _count:uint;
    public function Counter(  ) {
        _count = 0;
    }
    public function get count(  ):uint {
        return _count;
    }
    public function set count(value:uint):void {
        if(value < 100) {
            _count = value;
        }
        else {
            throw Error(  );
        }
    }
}

你可能感兴趣的:(actionscript3中的getter和setter的另一种写法)