親クラスフィールドを取得しようとしています。この例では、新しいDarkRoom
が作成されるとき、intゼロはDarkRoom
コンストラクターに渡されます。 thisRoomNumber
フィールドにゼロのintを代入することになっていました。次のステップ - Tenant
を作成し、初期化した1つのオブジェクトDarkRoom
を渡します。なんらかの理由でSemaphore
クラスのnextRoom
フィールドには、から継承されたフィールドthisRoomNumber
があり、という数値がに割り当てられていますが、値は96
です。何が欠けている?なぜ私は得ることができない0
からnextRoom.thisRoomNumber
デザインは非常に複雑です、多少単純な解決策がありますか?親クラスのフィールド値を得る親フィールドの代わりに
public abstract class Room {
public int thisRoomNumber = 96;
}
主な方法:
public class TestCase {
public static void main(String[] args) {
Room s[] = new Room[1];
{
s[0] = new DarkRoom(0, s);
}
new Tenant("Joe", s[0], s);
}
}
暗室クラス:
public class DarkRoom extends Room {
private Room thisRoomType = this;
public int thisRoomNumber;
private Room[] rooms;
private Semaphore semaphore= new Semaphore();
@Override
void leave(Tenant t) {
semaphore.release(thisRoomType, rooms, t);
}
public int thisRoomNumber;
public DarkRoom(int i, Room[] s) {
this.thisRoomNumber = i;
this.rooms = s;
}
}
セマフォクラス:
public class Semaphore {
public synchronized void release(Room thisRoom, Room[] allRooms, Tenant t) {
Room nextRoom;
while(j < allRooms.length &&
allRooms[j].negativeCounter == 0 &&
thisRoom.getClass().equals(allRooms[j].getClass())){
j++;
}
nextRoom = allRooms[j];
System.out.println(nextRoom.getClass().getName() +"' "+nextRoom.thisRoomNumber);
隠されたファイル* thisRoomNumber *(内部のみ)クラス* DarkRoom *からゼロを得ることができますか? –
正確には、このように2回定義した場合です。私はあなたがそれが親クラスで一度定義されることを意味すると思う。 –