如何使用方法传递接口实例参数?

时间:2015-04-11 06:12:05

标签: java collections interface instance

public void addLot(Parkable p ){
    //company.addLot(new ShortStayParkingLot("Short Stay Lot #1", 50));
    //company.addLot(new ShortStayParkingLot("Short Stay Lot #2", 25));
}

以上编码名为' ParkingSystem'它允许我添加一个新的停车场到下面的方法lot()。公司实例来自我的一个gui文件,

private ParkingSystem company = new ParkingSystem();
public java.util.Collection<Parkable> lots(){ 
    // Return the parking lots we have
    return null;
}

我有四个不同的停车场和每个停车场,每个构造函数都有name和initialCapacity实例,     public ShortStayParkingLot(java.lang.String name,int initialCapacity){         initialCapacity = Integer.MAX_VALUE;         this.initialCapacity = initialCapacity;         this.name = name;     }

public SimpleStatusPanel() {
    super();
    company.addLot(new ShortStayParkingLot("Short Stay Lot #1", 50));
    company.addLot(new ShortStayParkingLot("Short Stay Lot #2", 25));
    company.addLot(new ExpressParkingLot("Express Lot #1", 100));

    jcb = new JComboBox<Parkable>();
    for (Parkable p : company.lots()) {
        jcb.addItem(p);
    }
    add(buildOccupancyStatus());
}

我的问题是如何实现public void addLot(Parkable p)?你甚至可以拥有一个接口实例并将其作为参数吗?如果是这样,Parkable p会有哪种数据类型?

0 个答案:

没有答案