【问题标题】:AS3: How do I set a value of a class and prevent it from being changed?AS3:如何设置类的值并防止其被更改?
【发布时间】:2011-05-25 11:47:55
【问题描述】:

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

我正在使用 MVC,问题与我的模型有关。使用我的代码,我正在创建一个板,该板反过来创建瓷砖。棋盘上的每个图块都有一个 X 和一个 Y 值。在此之后,我想阻止对 setter 的访问,以防止自己再次意外更改值。

我正在考虑使用常量而不是变量,但似乎我必须在创建时定义值。换句话说: const myConst:uint;我的常量 = 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; }
}
}

【问题讨论】:

    标签: actionscript-3 oop model-view-controller


    【解决方案1】:

    假设您的变量永远不会像 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;
        } 
      }
    }
    

    【讨论】:

    • 谢谢。它看起来比我的解决方法干净得多。如果没有更好的答案出现,我想我会使用它。但是有一个问题:我听说在运行函数之前声明一个值是不好的,这有什么道理吗?
    • 我已经在 Flash 中制作游戏大约 3 年了,制作了 20 多个,这对我来说从来都不是问题。但这背后可能有一些神秘的隐藏原因,也许是管理方面的一些问题。但据我所知,即使是官方 Flex 也使用预设字段:)。或者使用 Number,设置为 NaN 并检查 isNaN()
    • 我明白了。我试图尽可能地按照规则编码,一旦我确切地知道为什么以某种方式做某事是常见的做法,就让自己更加宽容。我想这是我可以宽容一点的一件事。
    【解决方案2】:

    如果你想在创建时设置值,你必须为你的类定义一个显式的构造函数(即使在不需要时也总是建议)。

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

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

    【讨论】:

    • 同意。但必须有两个参数。他还有一个y
    • 我很难理解文本,因为我对编程比较陌生。如果您能写一个简短的代码示例,我将不胜感激。
    • 它有效。感谢您花时间编写示例。这对我帮助很大。起初我认为它不会起作用,因为我需要使用 getter 方法来检索值,但事实证明您可以使用 getter 而不需要 setter。
    【解决方案3】:

    我会像这样在构造函数中使用参数:

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

    【讨论】:

    • 谢谢,这确实是我现在用的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-01-10
    • 1970-01-01
    • 1970-01-01
    • 2022-12-13
    • 1970-01-01
    • 1970-01-01
    • 2017-08-02
    相关资源
    最近更新 更多