当试图比较作为参数传递的两个对象时:第一个作为对象,第二个作为对象数组(相同类型)。得到错误Incompatible conditional operand types Room and 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;
...
}
}
答案 0 :(得分:6)
请改为尝试:
while (j < allRooms.length && thisRoom.getClass().equals(allRooms[j].getClass()))
答案 1 :(得分:2)
你的逻辑错了。
当您传递对象实例 instanceof
时,ReferenceType
需要allRooms[j]
。这永远不会奏效
你应该做的是:
package
名称或class
名称或getClass
)进行比较。 根据您的需要选择