gpt4 book ai didi

java - 无法使用 instanceof 比较两个对象

转载 作者:行者123 更新时间:2023-12-03 19:38:39 25 4
gpt4 key购买 nike

当尝试比较作为参数传递的两个对象时:第一个作为对象,第二个作为对象数组(同类)。出现错误 不兼容的条件操作数类型 Room 和 allRooms[]。我试图在 while 循环中迭代,直到 thisRoomType 不等于 rooms 中的某个对象。如何解决?

public abstract class Room { ...}
public class LightRoom extends Room {...}
public class DarkRoom extends Room {...}

来电者:

release(thisRoomType, rooms)

带参数

private Room thisRoomType = this; // is defined in DarkRoom and LightRoom 
private Room[] rooms; // is defined in DarkRoom and LightRoom

在方法中:

public synchronized void release( Room thisRoom,  Room[] allRooms) {

try {
int j = 0;
while(thisRoom instanceof allRooms[j]){
jj++;
}
int nextRoom = jj;
...
}
}

最佳答案

试试这个:

while (j < allRooms.length && thisRoom.getClass().equals(allRooms[j].getClass()))

关于java - 无法使用 instanceof 比较两个对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10389755/

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