简体   繁体   English

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

[英]Can't create multiple instances of a class in Java

I'm trying to create multiple characters(squares) on the screen that move around randomly. 我正在尝试在屏幕上创建随机移动的多个字符(正方形)。 I have already created a CharMove class that creates a square, and moves it around randomly on the screen. 我已经创建了一个CharMove类,该类创建一个正方形,并在屏幕上随机移动它。 However, I tried creating multiple instances of this class in a seperate java file and only 1 instance was created. 但是,我尝试在单独的java文件中创建此类的多个实例,并且仅创建了1个实例。 What is wrong? 怎么了?

CharMove Class: CharMove类别:

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;
    } 
}

World 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);
}

However, I tried creating multiple instances of this class in a seperate java file and only 1 instance was created. 但是,我尝试在单独的java文件中创建此类的多个实例,并且仅创建了1个实例。

Nope, you're creating multiple instances. 不,您正在创建多个实例。 However, that doesn't make any difference because you don't have any per-instance state. 但是,这没有什么区别,因为您没有任何基于实例的状态。 Your only fields are these: 您唯一的字段是:

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

Those are static fields, which means they're not related to any specific instance of the class. 这些是静态字段,这意味着它们与该类的任何特定实例无关。 You probably just want to remove the static keyword from the declarations. 您可能只想从声明中删除static关键字。 (I'd also make the fields private and provide public getters/setters if necessary, but that's a different matter.) (我还将字段设为私有,并在必要时提供公共获取者/设置者,但这是另一回事。)

You'll also need to make your static methods into instance methods - because they're meant to act on individual instances, right? 您还需要使您的静态方法成为实例方法-因为它们是要作用于单个实例上的,对吗? Basically, I think you should revise the meaning of static via whatever book/tutorial you're using to learn Java. 基本上,我认为您应该通过用来学习Java的任何书籍/教程来修改static的含义。 (Also revise Java naming conventions.) (还修订了Java命名约定。)

You should not use public static void movement() as it is not instance method (well the name say it, it is static). 您不应该使用public static void movement()因为它不是实例方法(顾名思义,它是静态的)。 Matter of fact, you code should not be able to compile at char1.movement(100,100,game); 实际上,您的代码不应以char1.movement(100,100,game);进行编译char1.movement(100,100,game); . Should declare it as instance method public void movement() instead. 应该将其声明为实例方法public void movement() Actually for the rest of the method, you might want to do it that way too. 实际上,对于该方法的其余部分,您可能也想这样做。 Static work without instance of the class. 没有类实例的静态工作。

Your x and y are not instance variables, they are static variables. 您的x和y不是实例变量,它们是静态变量。 So every instance of CharMove shares the same x and y 因此,每个CharMove实例共享相同的x和y

Try this, 尝试这个,

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;
    } 
}

and

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)
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM