English 中文(简体)
消除公共变量和固定变量
原标题:Overriding a public variable with set

我有这样的基类。

class Base {
    public var space:Number;
}

延长期限

class Desc extends Base {

    override public function set space( space:Number ):void {
        //code
    } 

}

本文未经编辑。 Say,你没有控制基类,有什么办法执行同样的事?

显而易见的是建立了一套功能空间,但该物体正植根于已经存在的使用<代码>的公用空间的系统中。

提前感谢。

最佳回答

你们的基类应该这样界定:

class Base {

    // Don t use public variables. Make them private and define setters and getters 
    private var space_:Number;

    public function get space():Number {
        return space_;
    }

    public function set space(v:Number):void {
        if (space_ === v) return;
        space_ = v;
    }

}

这样,它就能够战胜:

class Desc extends Base {

    override public function set space( space:Number ):void {
        //code
    } 

}

<><>Edit>:

我错失了你所说的对基类的控制。 在这种情形下,希望space被定义为一个接头/秒(如果适当实施该舱)。 如果没有,你确实必须使用<条码>、植被空间和<条码>。

问题回答

From Adobe:

Class member variables that are declared using the var keyword cannot be overridden in a subclass.

采用一套办法在你的情况下不是一个选择。 如果你需要保持接口,则使用组成而不是继承:

class Desc {

  private var _base:Base;

  public function set space(space:Number):void {
    //code
    this._base.space = space;
  } 
}




相关问题
Attaching a property to an event in Flex/AS3

I have a parameter that needs to be passed along with an event. After unsuccessful attempts to place it on the type by extending the class, I ve been advised in another SO question to write a custom ...

Sorting twodimensional Array in AS3

So, i have a two-dimensional Array of ID s and vote count - voteArray[i][0] = ID, voteArray[i][1] = vote count I want the top 3 voted items to be displayed in different colors, so i have a 2nd Array -...

Virtual Tour using sketch up, ajax, flash technologies

I want to know if there are existing technology that make your 3d models in sketch into virtual tours, using either Ajax or Flash for web presentation. If there s none, which will be a good approach ...

What s a good way of deserializing data into mock objects?

I m writing a mock backend service for my flex application. Because I will likely need to add/edit/modify the mock data over time, I d prefer not to generate the data in code like this: var mockData =...

AS3 try/catch out of memory

I m loading a few huge images on my flex/as3 app, but I can t manage to catch the error when the flash player runs out of memory. Here is the what I was thinking might work (I use ???? because i dont ...

热门标签