gpt4 book ai didi

java - 面向对象编程 - 关于设计和访问相关数据的问题

转载 作者:行者123 更新时间:2023-12-04 06:12:43 25 4
gpt4 key购买 nike

考虑您在 Timer 中有以下代码:
它的主要目标是倒计时并在基于 Swing 的 GUI 上显示它。
计时器是游戏的一部分,用于通过让最先找到解决方案的用户来决定谁是赢家。

Action updateClockAction = new AbstractAction() {

public void actionPerformed(ActionEvent e) {

JLabel secLabel = m_GameApplet.GetJpanelStartNetGame().GetJlabelSeconds();
secLabel.setFont(new java.awt.Font("Lucida Handwriting", 1, 36));
secLabel.setForeground(Color.red);
secLabel.setText(Integer.toString(m_TimerSeconds));
if (m_TimerSeconds > 0) {
m_TimerSeconds--;
} else if (m_TimerSeconds == 0) {
m_Timer.stop();
m_GameApplet.GetJpanelStartNetGame().GetJlabelSeconds().setText("0");
m_GameApplet.GetJpanelStartNetGame().GetJbuttonFinish().setVisible(false);
//Checking whether time ended for both players and no solution was recieved
if (!m_WasGameDecisived) {
System.out.println("Tie - No one had a solution in the given time");
}
}
}
};
m_Timer = new Timer(1000, updateClockAction);

现在,我在客户端实现中有两个主要包
A.GUI - 保存所有摆动 Jpanels 等。
B.逻辑引擎

现在在 LogicEngine 中,我有一个名为 GameManager 的类——它应该管理和存储有关游戏的信息。

我目前的实现一般是这样的:
我有一个 JpanelMainGame这是在 GUI 包上 JpanelMainGame包含 JPanelGameBoard其中引用(或换句话说包含)一个 GameManger 的实例,这是在不同的包装上 - LogicEngine包裹。

所以我的问题是:
  • 上面所有的定时器定义代码应该放在哪里?

    A. 在 JpanelMainGame ? ( JpanelMainGame 应该有对它的引用)。

    B. 在 GameManger作为游戏数据的一部分
  • 在您提供的每个解决方案中,我应该如何从匿名内部类中访问所有标签信息?因为我只能使用命令从外部类访问成员:OuterClass.this.member .
  • 关于当前设计的任何评论。

  • 非常感谢

    最佳答案

    好吧,不要用问题来回答问题,但是...

  • 定时器多久使用一次?
  • Timer 编码是通用的还是特定于某个类的?

  • 如果 Timer 是通用的,那么我会说它属于 LogicEngine 包树中的某个地方。但是,如果 Timer 只能在 GUI 元素中使用,则它属于 GUI 包树。

    作为一般的经验法则(不要太用力地敲击敏捷鼓),您应该只编写现在有意义的代码,但不要害怕以后更改它。

    示例:您正在代码中创建类型为 AbstractAction() 的匿名内部类。如果简单地使用 updateClockAction 变量,这很好(尽管我回避匿名类)。但是,一旦您的编码导致您需要跨越 updateClockAction 的边界(通过 OuterClass.this.member),那么我断言是时候进行一些重构了:提取这个内部类并使其成为完全限定的类。这样,您可以对 updateClockAction 对象的内部状态进行适当的控制(使用 cstr、getter 等)

    编辑:添加了请求的示例
    public class Testing {
    private Timer timer; /* INIT this from somewhere.... */

    public void myFunction() {
    /* 60 Seconds */
    long countDownTimeSEC = 60;
    /* convert to Miliseconds */
    long countDownTimeMS = 1000 * countDownTimeSEC;

    /* Get ref to label */
    JLabel label = m_GameApplet.GetJpanelStartNetGame().GetJlabelSeconds();

    /* Set once */
    label.setFont(new java.awt.Font("Lucida Handwriting", 1, 36));
    label.setForeground(Color.red);

    /* Set initial time */
    label.setText(Long.toString(countDownTimeSEC));

    /* Get ref to button */
    JButton button = m_GameApplet.GetJpanelStartNetGame().GetJbuttonFinish();

    /* Set up post Count Down list */
    ArrayList<AbstractAction> actsWhenComplete = new ArrayList<AbstractAction>();

    /* instantiate countdown object */
    CountDownClockAction cdca = new CountDownClockAction(label, countDownTimeMS, actsWhenComplete);
    this.timer = new Timer(1000, cdca);

    /* Now that we have a timer, add the post Count Down action(s) to the post Count Down list */
    actsWhenComplete.add(new CountDownFinishAction(label, button, this.timer));


    /* Finally, kick off the timer */
    this.timer.start();
    }

    public static class CountDownClockAction extends AbstractAction {
    private static final long serialVersionUID = 1L;
    private final JLabel labelToUpdate;
    private final long startMS;
    private long currentMS;
    private long timeMark;
    private final ArrayList<AbstractAction> actionsToExeWhenComplete;
    private boolean actionsExedFlag;

    public CountDownClockAction(
    final JLabel labelToUpdate,
    final long startMS,
    ArrayList<AbstractAction> actionsToExeWhenComplete
    ) {
    super();
    this.labelToUpdate = labelToUpdate;
    this.startMS = startMS;
    this.currentMS = startMS;
    this.timeMark = 0;
    this.actionsExedFlag = false;
    this.actionsToExeWhenComplete = actionsToExeWhenComplete;
    }

    @Override
    public void actionPerformed(final ActionEvent e) {
    /* First time firing */
    if (this.timeMark == 0)
    this.timeMark = System.currentTimeMillis();

    /* Although the Timer object was set to 1000ms intervals,
    * the UpdateClockAction doesn't know this, nor should it
    * since > or < 1000ms intervals could happen to do the fact that
    * the JVM nor the OS have perfectly accurate timing, nor do they
    * have instantaneous code execution properties.
    * So, we should see what the *real* time diff is...
    */
    long timeDelta = System.currentTimeMillis() - this.timeMark;

    /* Allow for the label to be null */
    if (this.labelToUpdate != null)
    labelToUpdate.setText(Long.toString((long)(currentMS / 1000)));

    if (currentMS > 0) {
    currentMS -= timeDelta;

    } else if (currentMS <= 0 && this.actionsExedFlag == false) {
    /* Ensure actions only fired once */
    this.actionsExedFlag = true;
    /* Allow for the label to be null */
    if (this.actionsToExeWhenComplete != null)
    for (AbstractAction aa: this.actionsToExeWhenComplete)
    aa.actionPerformed(e);
    }

    /* Finally, update timeMark for next calls */
    this.timeMark = System.currentTimeMillis();
    }
    }

    public static class CountDownFinishAction extends AbstractAction {
    private final JLabel labelToUpdate;
    private final JButton buttonToUpdate;
    private final Timer timerToStop;

    public CountDownFinishAction(
    JLabel labelToUpdate,
    JButton buttonToUpdate,
    Timer timerToStop
    ) {
    super();
    this.labelToUpdate = labelToUpdate;
    this.buttonToUpdate = buttonToUpdate;
    this.timerToStop = timerToStop;
    }

    @Override
    public void actionPerformed(final ActionEvent e) {
    /* Perform actions, allowing for items to be null */
    if (this.labelToUpdate != null)
    this.labelToUpdate.setText("0");

    if (this.buttonToUpdate != null)
    this.buttonToUpdate.setVisible(false);

    if (this.timerToStop != null)
    this.timerToStop.stop();
    }
    }
    }

    关于java - 面向对象编程 - 关于设计和访问相关数据的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7586394/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com