AS3:如何设置类的值并防止其被更改?
感谢大家的回答找到了解决方案。检查这篇文章的底部。
我正在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您想在创建时设置该值,则必须为您的类定义一个显式构造函数(即使不需要,也始终建议这样做)。
您定义的构造函数基本上必须有一个参数,您可以通过该参数向内部属性提供一个值。这仅在实例初始化时完成一次。
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.
假设你的变量永远不会像 uint.MAX_VALUE 一样大:
Assuming your variables are NEVER going to be as big as uint.MAX_VALUE:
我会在构造函数中使用参数,如下所示:
或者如果您需要设置器:
I would use parameters in a constructor like this:
Or If you need a setter: