Actionscript Getter/setter 类 & OOP 中的范围

发布于 2025-01-07 02:24:01 字数 1506 浏览 7 评论 0原文

背景: 我最近在 AS3 中完成了一个 tic-tac-toe 游戏,使用了一些我多年前用 C 编写的简单的基于函数的代码。 现在,我正在寻求使用 OOP 技术和最佳实践以“正确的方式”做到这一点。 现在所有东西都被分成整齐的小包,看起来很漂亮,我旅程的最后一部分是让所有的小家伙互相交流。

我的困境: 我想将保存游戏状态的代码从我的 main 移动到 com.okaygraphics.model.GameState 中它自己的类。 问题是几乎所有其他包都会获取和设置这些游戏状态属性。 我试图找出封装这些东西的最简单方法,同时仍然允许我的其他类访问它。

我现在的位置:

package com.okaygraphics.model{

    public class GameState {

        private var _player1State:uint=0x00000000;
        private var _player2State:uint=0x00000000;
        private var _activePlayer:int=0;


        public function get p1GameState():uint {
            return _player1State;
        }
        public function set p1GameState(value:uint):void {
            _player1State = value;
        }

        public function get p2GameState():uint {
            return _player2State;
        }
        public function set p2gameState(value:uint):void {
            _player2State = value;
        }

        public function get activePlayer():int {
            return _activePlayer
        }
        public function set activePlayer(value:int):void {
            _activePlayer = value;
        }
    }
}

问题:

1)我需要构造函数吗?我的意思是,我的程序永远不会有多个 GameState。如果我应该将 getter/setter 调用为实例方法,那么如何让每个其他类从各自的包中引用相同的实例?

2)我还需要 getter 和 setter 吗?也许这个类只能有 3 个公共属性?如果是这样,我将如何实现其他课程的适当范围?

3)我应该使用 static 关键字将所有内容分配给类本身吗?如果是这样,我将如何实现和使用这些静态方法?

4)这是一个错误吗?我完全把自己逼到了角落吗?

这似乎是一项非常常见的任务,但我仍然不完全理解。简短的解释、一些链接或我需要的技术的名称将不胜感激。

提前致谢, -最大限度

Background:
I recently completed a tic-tac-toe game in AS3, using some simple function-based code I had written in C many years ago.
Now I'm on a quest to do it the "right way" using OOP techniques and best practices.
Everything is now divided into neat little packages, it looks pretty, and the last part of my journey is to get all the little buggers to communicate with each other.

My dilemma:
I want to move the code which holds the game state from my main to it's own class in com.okaygraphics.model.GameState.
The problem is nearly every other package gets and sets these game state properties.
I'm trying to figure out the simplest way to encapsulate this stuff, while still allowing my other classes to access it.

Where I'm at:

package com.okaygraphics.model{

    public class GameState {

        private var _player1State:uint=0x00000000;
        private var _player2State:uint=0x00000000;
        private var _activePlayer:int=0;


        public function get p1GameState():uint {
            return _player1State;
        }
        public function set p1GameState(value:uint):void {
            _player1State = value;
        }

        public function get p2GameState():uint {
            return _player2State;
        }
        public function set p2gameState(value:uint):void {
            _player2State = value;
        }

        public function get activePlayer():int {
            return _activePlayer
        }
        public function set activePlayer(value:int):void {
            _activePlayer = value;
        }
    }
}

Qestions:

1) Do I need a constructor? I mean, my program will never have more than one GameState. If I should call my getters/setters as instance methods, how do I get each other class to reference the SAME instance from their respective packages?

2) Do I even need getters and setters? Perhaps the class could just have 3 public properties? If so, how would I acheive the proper scope with regard to my other classes?

3) Should I assign everything to the class itself using the static keyword? If so, how would I implement and use those static methods?

4) Is this a mistake? Did I totally just program myself into a corner?

This seems like a pretty common task, yet one that I still don't fully understand. A brief explaination, some links, or the name of the technique I need would be greatly appreciated.

Thanks in advance,
-Max

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

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

发布评论

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

评论(3

随梦而飞# 2025-01-14 02:24:01

您可以通过几种不同的方式来解决这个问题。

您可以将 GameState 设为 Singleton。这将确保 GameState 的任何实例都是同一个实例。操作方法如下:

private static var _gameState:GameState;
private static var _allowInstantiation:Boolean;

public function GameState()
{
    if (!_allowInstantiation)
    {
        throw new Error("GameState is a Singleton. Use GameState.getInstance() instead of new GameState().");
    }
    else
    {
        _allowInstantiation = false;
    }
}

public static function getInstance():GameState
{
    if (!_gameState)
    {
        _allowInstantiation = true;
        _gameState = new GameState();
    }

    return _gameState;
}

然后,任何时候您想要获取 GameState 时,都可以使用以下语法:

var _gameState:GameState = GameState.getInstance();

通过使用 Singleton,您仍然可以使用已经创建的 getter 和 setter,但是 Singleton 将确保访问 GameState 的每个类都将访问同一个实例。

或者,如果您不想使用单例,您可以将这些私有变量改为公共静态变量:

public static var player1State:uint=0x00000000;
public static var player2State:uint=0x00000000;
public static var activePlayer:int=0;

您将不再需要 getter/setter,并且您可以从其他类中设置它们,如下所示:

GameState.player1State = 0xFF0000;
GameState.player2State = 0xFF0000;
GameState.activePlayer = 1;

无论哪种方式,您设置模型时,请记住考虑控制器和视图如何与其进行通信。请记住,模型和视图的分离是 MVC 模式的主要目标之一。

You could approach this several different ways.

You could make GameState a Singleton. That'll ensure that any instance of GameState is the same instance. Here's how to do that:

private static var _gameState:GameState;
private static var _allowInstantiation:Boolean;

public function GameState()
{
    if (!_allowInstantiation)
    {
        throw new Error("GameState is a Singleton. Use GameState.getInstance() instead of new GameState().");
    }
    else
    {
        _allowInstantiation = false;
    }
}

public static function getInstance():GameState
{
    if (!_gameState)
    {
        _allowInstantiation = true;
        _gameState = new GameState();
    }

    return _gameState;
}

Then, anytime you want to get GameState, you use this syntax:

var _gameState:GameState = GameState.getInstance();

By using a Singleton, you can still use the getters and setters you've already made, but the Singleton will ensure that every class that accesses GameState will be accessing the same instance.

Or, if you don't want to use a Singleton, you could make those private vars public static vars instead:

public static var player1State:uint=0x00000000;
public static var player2State:uint=0x00000000;
public static var activePlayer:int=0;

You'll no longer need the getters/setters, and you'd set them from other classes like this:

GameState.player1State = 0xFF0000;
GameState.player2State = 0xFF0000;
GameState.activePlayer = 1;

Either way you set up the Model, remember to put thought into how it'll be communicated with by the Controller and View. Remember that separation of Model and View is one of the main goals of the MVC pattern.

难以启齿的温柔 2025-01-14 02:24:01

1)你可以做的是创建单例类。这是一个只暗示过一次的课程。快速谷歌一下(在 stackoverflow 中搜索 AS3 Singleton),您应该会找到一些很好的单例类示例。对于单例,您只需要包含 GameState 类,并且您将访问唯一的一个。

2)稍后将 getter 和 setter 放在那里只会利大于弊。

3)无法真正回答。

4)不。似乎是要走的路。

1) What you could do is create singleton class. This is a class that is only intimated once. A quick google around (are search stackoverflow for AS3 Singleton) and you should find some good examples of a singleton class. With a singleton you only need to include the GameState class and you will be accessing the only one.

2) Can only do more good than harm having the getters and setters in there later on.

3) Can't really answer.

4) Nope. Seems like the way to go.

夜深人未静 2025-01-14 02:24:01

我强烈建议您查看 MVC设计模式并在您的游戏中使用它。 (游戏状态是模型的一部分。)

现在,至于如何从所有其他类(这是正确的词而不是包)访问游戏状态,您可以使用 单例模式

I highly recommend you to look at the MVC design pattern and to use it in your game. (The game state is part of the Model.)

Now, as to how to access the game state from all other classes (this is the correct word not packages) you can use the singleton pattern.

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