[英]Using an interface as a Value in a HashMap
我有一個用於住房系統的接口,如下所示:
public interface ITenant {
public TenantType getType();
public String toString();
}
TenantType()僅從Tenant類返回租戶的類型。
在House類中HashMap的聲明如下:
private Map<Room, ITenant> rooms = null;
“房間”用於檢查房間是否包含TenantType,如果包含,則表明該房間已被占用。
public int getAvailableRooms() {
int numOfFreeRooms = 0;
for (int i=0; i<numberOfRooms; i++) {
if (rooms.get(i) == null ) {
numOfFreeRooms += 1;
}
}
return numOfFreeRooms;
}
此方法通過使用HashMap返回多少個空閑房間。
我相信我了解接口如何工作,但不了解它們如何與HashMaps一起工作。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.