gpt4 book ai didi

java - 删除对象变量的所有实例

转载 作者:行者123 更新时间:2023-12-01 12:08:58 33 4
gpt4 key购买 nike

有没有办法删除对 Java 中特定对象的所有引用?

举个例子...tutor 对象有一个 Student 类型的属性,称为 myTutee

如果我正在使用 removeTutee 方法,该方法使用 Student 作为参数,有什么方法可以删除 myTutee 的值来自 tutor 对象的 属性,而不使用 setTutee

希望我能够很好地解释我想要实现的目标。非常感谢!

tutor.java

protected Student myTutee;

public int getTuteeId() {
return this.myTutee.getId();
}

public void setTutee(Student x) {
this.myTutee = x;
}

...

student.java

protected int studentId;

public int getId() {
return this.studentId;
}

...

classRoom.java

public void removeTutee(Student x) {
// can't reference any Tutor objects directly from here
// aiming to replicate tutor.setTutee(null)
// by only referring to Student x
}

最佳答案

如果没有对导师对象的引用,则无法从导师中删除学生(将其设置为 null)。如果您想实现这一目标,也许您的ER model是错的。想想如果 Tutor 链接到 Student 类,您可以执行 x.setTutee(null),这听起来也更好。另请参阅 ORM 策略。

如果您只有学生,您需要以某种方式至少获得类(class)导师的List。这样你就可以做到:

public void removeTutee(Student x) {
for(Tutor t: tutorList){
if(t.getStudent().getId() == x.getId()){
t.setTutee(null);
break;
}
}
}

关于java - 删除对象变量的所有实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27380515/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com