[英]How to automatically add an object to a list of another class?
So I'm currently working on a project for my Java class.所以我目前正在为我的 Java class 开发一个项目。
Here is my main:这是我的主要内容:
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
public class Main {
public static void main(String args[]){
Entrepot Entrepot1=new Entrepot(3);
Rangee Rangee1=new Rangee(2);
Rangee Rangee2=new Rangee(3);
Rangee Rangee3=new Rangee(3);
/*for(int i=0;i<Entrepot.getNbRangee();i++) {
Entrepot.addRangee(Rangee.getID()==i);
}*/
for(int i=0; i<Entrepot.getNbRangee(); i++) {
Entrepot.addRangee(Rangee.getID());
}
Meuble meuble = new Meuble("Table",new Lot[]{new Lot(new Vis(200,10),1),new Lot(new Planche(1000,500),3)},"Salon",3);
meuble.afficherMeuble();
System.out.println(Rangee1.getID());
System.out.println(Rangee2.getID());
System.out.println(Rangee3.getID());
System.out.println(Entrepot.getNbRangee());
System.out.println(Entrepot.getListeRangee());
}
}
Everytime the user creates an Object "Rangee" I want it to be automatically added in the list listeRangee inside the Entrepot class.每次用户创建 Object “Rangee”时,我希望它自动添加到 Entrepot class 内的 listeRangee 列表中。
As you can see on my Main, I tried a for loop where it adds each Rangee that has the ID of i, that didn't work out.正如你在我的 Main 上看到的,我尝试了一个 for 循环,它添加了每个 ID 为 i 的 Rangee,但没有成功。
I'm quite new at this so I'd really appreciate it if you can help me !我对此很陌生,所以如果您能帮助我,我将不胜感激!
Thanks !谢谢 !
Rangee.java Rangee.java
import java.util.ArrayList;
import java.util.List;
public class Rangee {
private static int count=0;
private static int ID;
private int longueur;
private int largeur=1;
private int hauteur=1;
private int volume= longueur*largeur*hauteur;
private ArrayList<Lot> listeLot= new ArrayList<Lot>();
public Rangee(int longueur) {
this.longueur=longueur;
if(ID<Entrepot.getNbRangee()) {
ID=count++;
}
else {
System.out.println("NON");
}
}
public static int getID() {
return ID;
}
public void setID(int iD) {
if(ID>Entrepot.getNbRangee()) {
System.out.println("Le nombre maximum de rangee est de "+ Entrepot.getNbRangee());
}
ID = iD;
}
Entrepot.java Entrepot.java
import java.util.ArrayList;
import java.util.List;
public class Entrepot {
private static int nbRangee;
static ArrayList<Rangee> listeRangee= new ArrayList<Rangee>(nbRangee);
public Entrepot(int nbRangee) {
this.nbRangee=nbRangee;
}
public static boolean addRangee( Rangee newRangee ) {
listeRangee.add(newRangee);
return true;
}
public static int getNbRangee() {
return nbRangee;
}
public void setNbRangee(int nbRangee) {
this.nbRangee = nbRangee;
}
public static ArrayList<Rangee> getListeRangee() {
return listeRangee;
}
public void setListeRangee(ArrayList<Rangee> listeRangee) {
this.listeRangee = listeRangee;
}
}
Make Rangee
's constructor private and expose a static factory method to build a new instance of Rangee
which will automatically add it to Entrepot
.将
Rangee
的构造函数设为私有并公开 static 工厂方法以构建Rangee
的新实例,该实例将自动将其添加到Entrepot
。
public Rangee {
private Rangee(int longueur) {
this.longueur=longueur;
if(ID<Entrepot.getNbRangee()) {
ID=count++;
} else {
System.out.println("NON");
}
}
public static Rangee createRangee(int longueur) {
Rangee rangee = new Rangee(longueur);
Entrepot.listeRangee.add(rangee);
return rangee;
}
You can even move that check if ID is lower than Entrepot.getNbRangee()
inside the createRangee
method and return null/throw error if that happens instead of printing a message.您甚至可以在
createRangee
方法中移动该检查 ID 是否低于Entrepot.getNbRangee()
并在发生这种情况时返回 null/throw 错误,而不是打印消息。
Then in your main method:然后在你的主要方法中:
Rangee rangee1 = Rangee.createRangee(2); // was Rangee Rangee1=new Rangee(2);
Rangee rangee2 = Rangee.createRangee(3); // was Rangee Rangee2=new Rangee(3);
Rangee rangee3 = Rangee.createRangee(3); // was Rangee Rangee3=new Rangee(3);
Everything else stays the same.其他一切都保持不变。
Note that createRangee
can be created in Entrepot
instead of Rangee
, but Rangee
's constructor will have to be accessible only to Entrepot
.请注意,可以在
Entrepot
而不是createRangee
中创建Rangee
,但Rangee
的构造函数必须只能由Entrepot
访问。 This means: 1/ making the constructor with default access(no public/private) and put both classes in the same package (other classes in the same package can still create Rangee
instances) or 2/ move Rangee
class inside Entrepot
as static class and keep the constructor private. This means: 1/ making the constructor with default access(no public/private) and put both classes in the same package (other classes in the same package can still create
Rangee
instances) or 2/ move Rangee
class inside Entrepot
as static class and保持构造函数私有。 With all that said, here is how I'd implement it in Entrepot
with package-private Rangee
constructor:综上所述,这是我在
Entrepot
中使用包私有Rangee
构造函数实现它的方式:
class Entrepot {
private int nbRangee;
private List<Rangee> listeRangee = new ArrayList<>(nbRangee);
public Entrepot(int nbRangee) {
this.nbRangee=nbRangee;
}
public int getNbRangee() {
return nbRangee;
}
public void setNbRangee(int nbRangee) {
this.nbRangee = nbRangee;
}
public List<Rangee> getListeRangee() {
return listeRangee;
}
public void setListeRangee(ArrayList<Rangee> listeRangee) {
this.listeRangee = listeRangee;
}
public Rangee createRangee(int longueur) {
if (listeRangee.size() < nbRangee) {
Rangee rangee = new Rangee(longueur);
listeRangee.add(rangee);
return rangee;
} else {
System.out.println("NON");
return null;
}
}
}
Note that I have removed a few statics here and there, since they make more sense now.请注意,我在这里和那里删除了一些静态,因为它们现在更有意义。 Note that you could set the
rangee
's id to listeRangee.size()
inside createRangee
.请注意,您可以在
rangee
的 id 设置为createRangee
listeRangee.size()
。 Your previous implementation updated a static variable and would have printed 3 for all your 3 rangees您之前的实现更新了 static 变量,并且将为您的所有 3 个范围打印 3
getId()`, which I suspect it isn't what you want. getId()`,我怀疑这不是你想要的。
Main method would be:主要方法是:
class Main {
public static void main(String[] args) {
Entrepot entrepot = new Entrepot(3);
Rangee rangee1 = entrepot.createRangee(2);
Rangee rangee2 = entrepot.createRangee(2);
Rangee rangee3 = entrepot.createRangee(3);
}
}
The easiest way I imagine to do this is to use the constructor of Rangee indirectly as a class method of Entrepot.我想最简单的方法是间接使用 Rangee 的构造函数作为 Entrepot 的 class 方法。
Inside the Entrepot class you can add this (you could do it with parameters if you need to):在 Entrepot class 中,您可以添加它(如果需要,您可以使用参数来完成):
public void createRangee(){
Rangee aux = new Rangee();
listeRangee.add(newRangee);
}
Then in your main you wouldn't have to call the constructor, but instead this method:然后在你的主要你不必调用构造函数,而是这个方法:
Entrepot ntrepot1=new Entrepot(3);
entrepot1.createRangee(); // The number of times you need
As for the id of each you could use this solution:至于每个人的 id 你可以使用这个解决方案:
private static AtomicInteger ID_GENERATOR = new AtomicInteger(1000);私有 static AtomicInteger ID_GENERATOR = new AtomicInteger(1000);
Rangee aux = new Rangee(ID_GENERATOR.getAndIncrement());
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.