AS3:如何设置类的值并防止其被更改?

发布于 2024-10-07 06:59:32 字数 1114 浏览 4 评论 0原文

感谢大家的回答找到了解决方案。检查这篇文章的底部。

我正在使用 MVC,问题与我的模型有关。我用我的代码创建了一个板,该板又创建了图块。棋盘上的每个图块都有一个 X 和一个 Y 值。此后,我想阻止访问设置器,以防止自己再次意外更改该值。

我正在考虑使用常量而不是变量,但似乎我必须在创建时定义该值。换句话说: const myConst:uint; myConst = 2; // 不起作用

现在我有一个我不满意的解决方法。当然有一种更清洁的方法。您可以在下面看到我的解决方法。

package myboardgame
{
 internal class Tile
 {
  private var _x:uint;
  private var _y:uint;

  private var _xLock:Boolean; // Makes sure that the X and Y values of a tile can only be set once to prevent errors
  private var _yLock:Boolean; //  " " 

  internal function set x(x:uint):void
  { if(!_xLock) {_x = x; _xLock = true;} else { throw new Error("Trying to change the one-time write access X tile value")}}
  internal function get x():uint
  { return _x; }
 }
}

编辑。我采用的解决方案:

package myboardgame
{
    internal class Tile
{
    private var _x:uint;
    private var _y:uint;

    public function Tile(x:uint, y:uint):void
    {
        _x = x;
        _y = y;
    }
    internal function get x():uint
    { return _x; }

    internal function get y():uint
    { return _y; }
}
}

Solution found thanks to everyone's answers. Check bottom of this post.

I am using MVC and the problem concerns my model. With my code I am creating a board which in turn creates tiles. Every tile on the board gets an X and a Y value. After this I want to prevent access to the setter to prevent myself from accidentally changing the value ever again.

I was thinking of using a constant instead of a variable, but it seems I have to define the value at the time of creation. In other words: const myConst:uint; myConst = 2; // does not work

Right now I have a work-around which I am not happy with. Surely there's a cleaner way. You can see my work-around below.

package myboardgame
{
 internal class Tile
 {
  private var _x:uint;
  private var _y:uint;

  private var _xLock:Boolean; // Makes sure that the X and Y values of a tile can only be set once to prevent errors
  private var _yLock:Boolean; //  " " 

  internal function set x(x:uint):void
  { if(!_xLock) {_x = x; _xLock = true;} else { throw new Error("Trying to change the one-time write access X tile value")}}
  internal function get x():uint
  { return _x; }
 }
}

Edit. The solution I went with:

package myboardgame
{
    internal class Tile
{
    private var _x:uint;
    private var _y:uint;

    public function Tile(x:uint, y:uint):void
    {
        _x = x;
        _y = y;
    }
    internal function get x():uint
    { return _x; }

    internal function get y():uint
    { return _y; }
}
}

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

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

发布评论

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

评论(3

岁月静好 2024-10-14 06:59:32

如果您想在创建时设置该值,则必须为您的类定义一个显式构造函数(即使不需要,也始终建议这样做)。

您定义的构造函数基本上必须有一个参数,您可以通过该参数向内部属性提供一个值。这仅在实例初始化时完成一次。

public class Tile {

   //these are the attributes: your instance status
   private var x:int;
   private var y:int;

   //this is the class constructor
  public function Tile(_x:int, _y:int){
      //here goes the initialization of your attributes and other stuff you may need
      x = _x;
      y = _y;
   }

   //then the other methods... 
}

If you want to set the value at the creation time you must define an explicit constructor for your class (always suggested even when not needed).

The constructor you define must basically have one parameter by means of which you can provide a value to the inner attribute. This is done only once at the instance initialization.

public class Tile {

   //these are the attributes: your instance status
   private var x:int;
   private var y:int;

   //this is the class constructor
  public function Tile(_x:int, _y:int){
      //here goes the initialization of your attributes and other stuff you may need
      x = _x;
      y = _y;
   }

   //then the other methods... 
}
清引 2024-10-14 06:59:32

假设你的变量永远不会像 uint.MAX_VALUE 一样大:

package{
  public class MyClass{
    private var _x:uint = uint.MAX_VALUE;
    public function set x(x:uint){
      if (x != MAX_VALUE)
        //error
      _x = x;
    } 
  }
}

Assuming your variables are NEVER going to be as big as uint.MAX_VALUE:

package{
  public class MyClass{
    private var _x:uint = uint.MAX_VALUE;
    public function set x(x:uint){
      if (x != MAX_VALUE)
        //error
      _x = x;
    } 
  }
}
我不吻晚风 2024-10-14 06:59:32

我会在构造函数中使用参数,如下所示:

var Tile : Tile = new Tile(3,6);

或者如果您需要设置器:

package
{
  public class Tile
  {
    private var _x : uint;
    private var _isSetX : Boolean;
    private var _y : uint;
    private var _isSetY : Boolean;

    public function set x(value : uint)
    {
      if (_isSetX)
        return;
      
      _x = value;

    }

   .......


  }
}

I would use parameters in a constructor like this:

var Tile : Tile = new Tile(3,6);

Or If you need a setter:

package
{
  public class Tile
  {
    private var _x : uint;
    private var _isSetX : Boolean;
    private var _y : uint;
    private var _isSetY : Boolean;

    public function set x(value : uint)
    {
      if (_isSetX)
        return;
      
      _x = value;

    }

   .......


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