【问题标题】:Separate a function to a class in Java在 Java 中将函数与类分离
【发布时间】:2014-10-19 13:13:03
【问题描述】:

我的教授让我用 AI 制作棋盘游戏,但我希望 AI 与主代码分开。我想要做的就是将 AI.class 放到 Game.class 所在的同一个文件夹中,这样每次执行下面代码中的 AI() 函数时,Game.class 都会调用 AI.class AI.class 也将被执行。我还将 int 数组的值提供给 AI.class,AI.class 将返回四个整数。 PS我们不能用netbeans,只能用notepad或者textpad之类的。

            @SuppressWarnings("serial")

            public class Game extends JPanel {

            BufferedImage board;
            int[] intinfo = new int[]{486, 539, 591,643,696,749,801,853,907}; 
            int intinfo1;
            int intinfo2;
            int intinfo3;
            int intinfo4;

            char keyPressed;

                //I want this to be on different class
                AI(){
                    int[] info=new int[10]; //the values of this will come from intinfo
                    info1=1; //values of these four info will be returned to the intinfo
                    info2=2;
                    info3=3;
                    info4=4;
                }

                public Game() {
                    KeyListener listener = new KeyListener() {
                        @Override
                        public void keyTyped(KeyEvent e) {
                        }
                        @Override
                        public void keyPressed(KeyEvent e) {
                            keyPressed= e.getKeyChar();
                        }

                        @Override
                        public void keyReleased(KeyEvent e) {
                        }
                    };
                    addKeyListener(listener);
                    setFocusable(true);
                }

                @Override
                public void paint(Graphics g) {
                    super.paint(g);
                    g.drawImage(board, 0, 0,this);
                }

                public static void main(String[] args) throws InterruptedException {
                    JFrame frame = new JFrame("Games of the Generals");
                    Game game = new Game();
                    frame.add(game);
                    frame.setSize(1040, 680);
                    frame.setVisible(true);
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    game.repaint();

                    while(true){
                        game.AI();
                        Thread.sleep(50);
                    }
                }
            }

【问题讨论】:

  • 所以试试你刚才说的,看看会发生什么......
  • 如果您不允许从 IDE 获得帮助,为什么您认为允许来自 SO 的帮助?
  • 这更像是一个 java101 问题,而不是一个非常有价值的问题。你有一个特定的任务,但这不是一个“问题”。先自己尝试,如果在分离过程中遇到特定问题,请返回 SO。

标签: java class


【解决方案1】:

当然,你需要做的就是

public class Game extends JPanel 
{
    private AI ai;
    int[] intinfo = new int[]{486, 539, 591,643,696,749,801,853,907}; 

    public Game()
    {
        this.ai = new AI();
        ...
    }

    public void execute()
    {
        ai.AI(intinfo);
    }
}

...
                game.repaint();

                while(true)
                {
                    game.execute();
                    Thread.sleep(50);
                }

当然还有AI.java

public class AI
{

    int intinfo1;
    int intinfo2;
    int intinfo3;
    int intinfo4;

    public void AI(int[] intinfo)
    {
        int[] info = new int[10]; //the values of this will come from intinfo
        info1=1; //values of these four info will be returned to the intinfo
        info2=2;
        info3=3;
        info4=4;
    }

    ...
}

你需要用 javac 编译这两个类 - how to compile multiple java source files in command line

【讨论】:

  • 试过了但有错误:找不到符号ai.AI();
  • 哎呀,我完全搞砸了一些琐碎的事情,秒 - 更改了 AI 类中的某些内容以修复它(添加 void
  • 那行得通,谢谢 :) 虽然我希望 AI.java 中的 intinfo 位于 Game.java 中,并将 Game.java 中的 intinfo 传递给 Ai.java
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-06-16
  • 1970-01-01
  • 1970-01-01
  • 2015-10-24
  • 1970-01-01
相关资源
最近更新 更多