简体   繁体   English

如何创建同一类的多个对象?

[英]How do I create multiple objects of the same class?

I want to create two objects of the Ball class. 我想创建Ball类的两个对象。 I have tried the following: 我尝试了以下方法:

public class World extends JPanel {
    JFrame frame = new JFrame("GreenJ");
    Actor[]  actor = new Actor[100];
    int n = 0;
    public World() throws InterruptedException{
        frame.add(this);
        frame.setSize(1000, 1000);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    public void addObject(Actor a) {
        actor[n] = a;
        frame.add(actor[n]);
    }
}


public class MyWorld extends World {
    public MyWorld() throws InterruptedException {
        addObject(new Ball(frame, 250, 750));
        addObject(new Ball(frame, 750, 250)); 
    }
}


public class Ball extends Actor{
    int x;
    int y;

    @Override
    public void paint(Graphics g) {
        super.paint(g);
        Graphics2D g2d = (Graphics2D) g;

        g2d.fillOval(x, y, 50, 50);
    }

    public Ball(JFrame frame, int a, int b) throws InterruptedException{  
        frame.add(this);

        x = a;
        y = b;
    }

    public void main(String[]Args) {
        repaint();
    }
}

When I run this code I only get the first 'ball' in my frame. 当我运行这段代码时,我只会在框架中得到第一个“球”。 I have tried some other things but without success. 我尝试了其他方法,但没有成功。

Thank you in advance. 先感谢您。 ElAdriano ElAdriano

The value of n is never changed in your code. n的值永远不会在您的代码中更改。 So addObject will always put the new object in index 0 of your actor array. 因此, addObject始终会将新对象放在actor数组的索引0中。

Change your Actor[] into ArrayList of type Actor this would help you forget about where to add the next object or at any index n 改变你的Actor[]进入ArrayList类型的Actor ,这将帮助你忘记在哪里添加的下一个对象或任何索引n

ArrayList<Actor> actors = new ArrayList<>();

and change the addObject() method to add the object to the actors array 并更改addObject()方法以将对象添加到actors数组

addObject(Actor a){
    actors.add(a);
}

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

相关问题 如何使用Configuration Admin为同一类创建多个配置? - How do I create multiple configurations for the same class with Configuration Admin? 如何在Java中创建多个对象? - How do I create multiple objects in Java? 如何使用父 class 构造函数(super())为子类创建多个对象? - How do i create multiple objects for child classes using parent class constructor(super())? 一种创建相同类的多个对象的简便方法 - An easier way to create multiple objects of the same class 如何为具有相同内容的多个片段创建片段模板(具有变量和方法的类、布局)? - How do I create a fragment template(class with variables and methods, layout) for multiple fragments with the same content? 如何在Java的同一数组中存储多级类对象? - How do I store multipule class objects in the same array in Java? 如何在不知道所需对象数量的情况下为同一类创建多个对象? - How can you create multiple objects for the same class without knowing the amount of objects needed? 如何在同一个类中创建 n 个异常? - How do I create n-number exceptions in the same class? 如何处理列表中的对象,我能否在循环中创建多个具有不同名称的对象? - How do I work with objects in lists and can I create multiple objects with different names in a loop? 如何创建扩展类的多个对象 - How to create multiple objects of an extended class
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM