ActionScript - Read Only Property and Private Set Method?

蓝咒 提交于 2019-12-01 11:37:29

The MXML compiler does not support getters and setters with mixed scopes/namespaces. There are a few tickets open regarding this:

It's quite annoying, but at least Adobe is aware of it. There is a way to accomplish mixed namespace getters and setters by using custom namespaces and fully-qualifying references to the getter or setter.

package {

    use namespace my_namespace

    public class MyClass {

        private var _name:String;

        public function get name():String {
            return _name;
        }

        my_namespace function set name(value:String):void {
            _name = value;
        }
     }

     public class MySubClass extends MyClass {

       public function MySubClass(name:String) {
           super.my_namespace::name = name;
       }
    }
  }
}

Is there a reason

private function set myNumber(value:Number):void
{
    myNumberProperty = value;
}

doesn't work? What error does it give? I've done this all the time in Flex, so I'm not sure if it only works there... I wouldn't think so, though.

edit: Looks like this is a compiler bug. Here's a blog post with the solution http://blogagic.com/230/struggling-with-flex-error-1000-ambiguous-reference-to

Not ideal, but could you just create a private method?

> is there no way to use the set keyword
> on a private function?<br/>

Nope If one is private they both have to be private. Regardless of what others think this is not a bug.

The idea behind setters/getters is to isolate code from the public.
Remember this OOP
You should also try to stay with typical convention for the var name with a leading _

private var _myNumber:Number

// private assessor/assignor 
  private function set number(value:Number):void{
    this._myNumber= Math.max(0, Math.min(value, 100));
  }
  private function get number():void{
    return this._myNumber;
  }


// public assessor
  public function get myNumber():Number{
    return this._myNumber;
  }


refer
The so called bug report is here

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!