这是我的Car类:
public class Car {
private int FGNr;
private String name;
private String type;
private Owner owner;
private static ArrayList<Integer> allCarIds = new ArrayList<>();
public Car(int FGNr, String name, String type, Owner o) throws Exception {
setFGNr(FGNr);
setName(name);
setType(type);
setOwner(o);
}
public int getFGNr() {
return FGNr;
}
public void setFGNr(int FGNr) throws Exception{
this.FGNr = FGNr;
if(allCarIds.contains(this.FGNr))
throw new Exception("FGNr already excists!! ");
allCarIds.add(this.FGNr);}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public Owner getOwner() {
return owner;
}
public void setOwner(Owner owner) throws Exception{
owner.addCar(this);
this.owner = owner;
}
@Override
public int hashCode() {
int hash = 7;
hash = 73 * hash + this.FGNr;
return hash;
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final Car other = (Car) obj;
if (this.FGNr != other.FGNr) {
return false;
}
return true;
}
@Override
public String toString() {
return "Car{" + "FGNr=" + FGNr + ", name=" + name + ", type=" + type + ", owner=" + owner + '}';
}
}
这是我的所有者类:
public class Owner {
private String SVNr;
private String name;
HashSet<Car> allCars = new HashSet<>();
private static ArrayList<String> allOwnerSVNs = new ArrayList<>();
public Owner(String SVNr, String name) throws Exception{
setSVNr(SVNr);
setName(name);
}
public void addCar(Car c) throws Exception{
if(allCars.contains(c))
throw new Exception("this user has already this car");
if(c.getOwner()!=null)
throw new Exception("this car belongs to other owner");
c.setOwner(this);
allCars.add(c);
}
public String getSVNr() {
return SVNr;
}
public void setSVNr(String SVNr) throws Exception{
this.SVNr = SVNr;
if(allOwnerSVNs.contains(this.SVNr))
throw new Exception("SVNg already excists!! ");
allOwnerSVNs.add(this.SVNr);
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public HashSet<Car> getAllCars() {
return allCars;
}
@Override
public int hashCode() {
int hash = 5;
hash = 41 * hash + Objects.hashCode(this.SVNr);
return hash;
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final Owner other = (Owner) obj;
if (!Objects.equals(this.SVNr, other.SVNr)) {
return false;
}
return true;
}
@Override
public String toString() {
return "Owner{" + "SVNr=" + SVNr + ", name=" + name + ", allCars=" + allCars + '}';
}
}
这是我的主要内容:
try {
Owner o1 = new Owner("0001","Owner1");
Owner o2 = new Owner("0002","Owner2");
Car c1 = new Car(1,"Model S", "Tesla",o1);
Car c2 = new Car(2,"Model 3", "Tesla",o2);
Car c3 = new Car(3,"TT", "Audi",o2);
} catch (Exception ex) {
System.out.println("error:"+ex.getMessage());
}
因此,在尝试创建新车时,我收到此错误:
Exception in thread "main" java.lang.StackOverflowError
at java.util.HashMap.containsKey(HashMap.java:595)
at java.util.HashSet.contains(HashSet.java:203)
at pkgData.Owner.addCar(Owner.java:28)
at pkgData.Car.setOwner(Car.java:63)
...........
这是一个递归错误,但我不知道如何解决它。如果我创建一辆新车显然我必须将车添加到汽车的所有者arrayList。如果我调用addCar函数,该函数将调用getOwner函数。这是一个无休止的呼唤圈。
如何确保在创建新车时还会更改所有者的集合。没有任何意义的汽车拥有车主,但汽车的拥有者不是他的收藏中的汽车。
答案 0 :(得分:1)
如你所见,这两个函数属于无限循环。
在汽车课上
public void setOwner(Owner owner) throws Exception{
owner.addCar(this);
this.owner = owner;
}
并且在所有者类
中public void addCar(Car c) throws Exception{
if(allCars.contains(c))
throw new Exception("this user has already this car");
if(c.getOwner()!=this && c.getOwner()!=null)
throw new Exception("this car belongs to other owner");
c.setOwner(this);
allCars.add(c);
}
汽车设置它的主人并将自己发送给所有者类&#39; addCar()
方法,没关系。但是,为什么所有者班级&#39; addCar()
方法将所有者设置为再次?
我认为存在逻辑错误。如果您删除c.setOwner(this)
行,则可以正常使用。
答案 1 :(得分:0)
通常在容器中添加元素应该在容器本身中完成。
在您的示例中,Owner
是容器,Car
是元素。
例如,请参阅java.awt.Container
和java.awt.Component
。
请勿从owner.addCar(this);
致电Car.setOwner
。
让Owner
将汽车添加到列表中(您已经这样做),并将自己设置为Car
的所有者。
public void setOwner(Owner owner) throws Exception{
owner.addCar(this); //remove this line
this.owner = owner;
}