开发者

ActionScript - Read Only Property and Private Set Method?

开发者 https://www.devze.com 2023-02-20 22:10 出处:网络
one thing i\'ve never really understood about AS3 is that you can\'t have a private set method and a public get method together.

one thing i've never really understood about AS3 is that you can't have a private set method and a public get method together.

from within my class i would like to assign values that would call a private set function:

myNumber = 22;

but i need to pass that number as a parameter to a function

myNumber(22);

for example:

package
{
//Imports
import flash.display.Sprite

//Class
public class NumberClass extends Sprite
    {
    //Properties
    private var myNumberProperty:Number

    //Constructor
    public function NumberClass(myNumber:Number):void
        {
        this.myNumber = myNumber;

        init();
        }

    //Initialize
    private function init():void
        {
        trace(myNumber);
        }

    //My Number Setter
    private function set myNumber(value:Number):void
        {
        myNumberProperty = Math.max(0, Math.min(value, 100));
        }

    //My Number Getter
    public 开发者_开发问答function get myNumber():Number
        {
        return myNumberProperty;
        }
    }
}

is there no way to use the set keyword on a private function?


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

  • https://bugs.adobe.com/jira/browse/ASL-44
  • https://bugs.adobe.com/jira/browse/SDK-25646
  • https://bugs.adobe.com/jira/browse/ASL-112.

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

0

精彩评论

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