无法在 Java 中创建 class 的多个实例

Can't create multiple instances of a class in Java

我正在尝试在屏幕上创建多个随机移动的角色(方块)。我已经创建了一个 CharMove class 来创建一个正方形,并在屏幕上随机移动它。但是,我尝试在单独的 java 文件中创建此 class 的多个实例,但只创建了 1 个实例。怎么了?

CharMove Class:

public class CharMove extends JPanel {
    public static int x = 250;
    public static int y = 250;

    public void paint(Graphics g) {
        Graphics pane = (Graphics2D) g;
        pane.setColor(Color.blue);
        pane.fillRect(x, y, 10, 10); 

    }

    public static void movement(int x, int y, JFrame frame) { 
        CharMove.x = x; 
                CharMove.y = y;
        while (true) {
            try {
                TimeUnit.SECONDS.sleep(1);
                CharMove.x = Getx(CharMove.x,frame); 
                CharMove.y = Gety(CharMove.y,frame);
                frame.repaint();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    public static int Getx(int a, JFrame frame) { 
        Random rn = new Random();
        int xnum = rn.nextInt(10)-5; 
        a += xnum; 
        System.out.println("x:" + a); 
        return a;
    } 
    public static int Gety(int b, JFrame frame){ 
        Random rn = new Random();
        int ynum = rn.nextInt(10)-5; 
        b += ynum; 
        System.out.println("y:" + b); 
        return b;
    } 
}

世界Class

public static void main(String[] args) {
    JFrame game = new JFrame();
    game.setTitle("Matrix");
    game.setSize(500, 500);;
    game.getContentPane().setBackground(Color.white);
    game.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    game.setVisible(true);  
    CharMove char1 = new CharMove(); 
    CharMove char2 = new CharMove();
    game.add(char1);   
    game.add(char2);
    char1.movement(100,100,game); 
    char2.movement(250,250,game);
}

你不应该使用 public static void movement() 因为它不是实例方法(顾名思义,它是静态的)。事实上,您的代码应该无法在 char1.movement(100,100,game); 处编译。 应该将其声明为实例方法 public void movement() 而不是。实际上,对于该方法的其余部分,您可能也想这样做。没有 class.

实例的静态工作

However, I tried creating multiple instances of this class in a seperate java file and only 1 instance was created.

不,您正在创建多个实例。但是,这没有任何区别,因为您没有任何实例状态。您唯一的字段是这些:

public static int x = 250;
public static int y = 250;

这些是 static 字段,这意味着它们与 class 的任何特定实例无关。您可能只想从声明中删除 static 关键字。 (我也会将这些字段设为私有,并在必要时提供 public getters/setters,但这是另一回事。)

您还需要将静态方法变成实例方法 - 因为它们旨在作用于单个实例,对吗?基本上,我认为你应该通过你用来学习 Java 的任何 book/tutorial 修改 static 的含义。 (同时修改 Java 命名约定。)

你的 x 和 y 不是实例变量,它们是静态变量。所以 CharMove 的每个实例共享相同的 x 和 y

试试这个,

public class CharMove extends JPanel {
    private int x = 250;
    private int y = 250;

    public void paint(Graphics g) {
        Graphics pane = (Graphics2D) g;
        pane.setColor(Color.blue);
        pane.fillRect(x, y, 10, 10); 

    }

    public void movement(JFrame frame) { 
        while (true) {
            try {
                TimeUnit.SECONDS.sleep(1);
                this.x = CharMove.Getx(this.x,frame); 
                this.y = CharMove.Gety(this.y,frame);
                frame.repaint();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    public static int Getx(int a, JFrame frame) { 
        Random rn = new Random();
        int xnum = rn.nextInt(10)-5; 
        a += xnum; 
        System.out.println("x:" + a); 
        return a;
    } 
    public static int Gety(int b, JFrame frame){ 
        Random rn = new Random();
        int ynum = rn.nextInt(10)-5; 
        b += ynum; 
        System.out.println("y:" + b); 
        return b;
    } 
}

public static void main(String[] args) {
    JFrame game = new JFrame();
    game.setTitle("Matrix");
    game.setSize(500, 500);;
    game.getContentPane().setBackground(Color.white);
    game.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    game.setVisible(true);  
    CharMove char1 = new CharMove(); 
    CharMove char2 = new CharMove();
    game.add(char1);   
    game.add(char2);
    char1.movement(game); 
    char2.movement(game)
}