ActionScript - 只读属性和私有设置方法?

发布于 2024-10-26 21:30:28 字数 939 浏览 7 评论 0原文

我从未真正理解 AS3 的一件事是,不能同时使用私有 set 方法和公共 get 方法。

在我的班级中,我想分配调用私有集函数的值:

myNumber = 22;

但我需要将该数字作为参数传递给函数,

myNumber(22);

例如:

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;
        }
    }
}

是否无法在私有函数上使用 set 关键字?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

缱绻入梦 2024-11-02 21:30:28

MXML 编译器不支持具有混合作用域/命名空间的 getter 和 setter。有一些与此相关的票证:

这很烦人,但至少 Adob​​e 意识到了这一点。有一种方法可以通过使用自定义命名空间和对 getter 或 setter 的完全限定引用来实现混合命名空间 getter 和 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;
       }
    }
  }
}

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;
       }
    }
  }
}
波浪屿的海角声 2024-11-02 21:30:28

有什么理由

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

不行吗?它给出了什么错误?我一直在 Flex 中这样做,所以我不确定它是否只在那里工作......但我不这么认为。

编辑:看起来这是一个编译器错误。这是一篇博客文章,其中包含解决方案 http://blogagic。 com/230/struggling-with-flex-error-1000-ambiguously-reference-to

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

才能让你更想念 2024-11-02 21:30:28

不太理想,但是您可以创建一个私有方法吗?

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

提笔书几行 2024-11-02 21:30:28
> is there no way to use the set keyword
> on a private function?<br/>

不,如果一个是私有的,那么它们都必须是私有的。无论其他人如何认为这都不是一个错误。

setter/getter 背后的想法是将代码公共隔离。
记住这个 OOP
您还应该尝试遵循带有前导 _

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;
  }

的 var 名称的典型约定
参考
所谓的错误报告在这里

> 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文