繁体   English   中英

在HashMap中使用接口作为值

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM