工厂模式/ ArrayList /接口问题

时间:2015-02-10 01:44:48

标签: java arraylist interface factory-pattern

我使用工厂模式使用ElevatorMover界面创建不同类型的电梯。工厂将电梯存放在ArrayListElevatorMover个对象中。当我从ElevatorArrayList.get(i)检索电梯[ArrayList]时,我无法调用PassengerElevator方法。只有ElevatorMover方法(当然没有实现)。

我在这里做不到什么?

这是我试图在主

中调用的代码
// make elevator 1 go to the 11th floor
testBuilding.getElevator(1).moveUp(11);

这是我的界面

public interface ElevatorMover {

public void moveUp(int i);

public void moveDown(int i);

public void openDoors();

这是PassengerElevator方法

@Override
public void moveUp(int i) {
    while (currentFloor != i) {
        setCurrentFloor(currentFloor++);
    }
}

错误消息

Exception in thread "main" java.lang.NullPointerException
    at Elevator.Building.getElevator(Building.java:109)
    at Elevator.ElevatorSimulatorMain.main(ElevatorSimulatorMain.java:34)

Building.class构造函数

private ArrayList<Floor> floorArrayList; // the ArrayList of floors in the building
private ArrayList<ElevatorMover> elevatorArrayList; // the ArrayList of elevators in the building

public Building(int numFloors, int numElevators) {

    this.numFloors = numFloors;
    this.numElevators = numElevators;

    // create each floor
    if (numFloors > 0) // Must have 1 or more floors
    {
        for (int i = 1; i <= numFloors; i++) {
            floorArrayList.add(new Floor(i));
        }
    }
    else 
    {
        System.out.println("Building must have 1 or more floors.");
    }

    // create each elevator.
    if (numElevators > 0) //  Must have 1 or more elevators
    {
        for (int i = 1; i <= numElevators; i++) {
        elevatorArrayList.add(ElevatorFactory.build("Passenger", i));
        }
    }
    else
    {
        System.out.println("Building must have 1 or more elevators.");
    }
}

ElevatorFactory构建方法

public static ElevatorMover build(String type, int elevID) {

    if (type.equals("Passenger")) {
        return new PassengerElevator(elevID);
    }
    else return null; // don't know what this is
}

getElevator方法

public ElevatorMover getElevator(int i) {
    return elevatorArrayList.get(i);
}

2 个答案:

答案 0 :(得分:1)

我猜你没有创建elevatorArrayList。示例

List<ElevatorMover> elevatorArrayList = new ArrayList<ElevatorMover>();

private ArrayList<ElevatorMover> elevatorArrayList; // This is just declaring not creating elevatorArrayList

答案 1 :(得分:0)

当你调用一个接口方法 - 实际上任何方法(静态方法除外)时,被调用的方法是实际类中的方法(或尽可能接近它) - 而不是引用类型中的一个。

考虑以下代码:

class A {
    public void doStuff() {
        System.out.println("In A");
    }
}
class B extends A {
    public void doStuff() {
        System.out.println("In B");
    }
}
class C extends B {
    public void doStuff() {
        System.out.println("In C");
    }
}

class Main {
    public static void main(String[] args) {
        A a = new C();
        a.doStuff(); // Prints "In C", NOT "In A", because the object is a C, even though the variable's type is "reference to A"
    }
}

同样,如果您引用PassengerElevator并致电moveUp,那么无论参考是什么类型,它都会调用moveUp中的PassengerElevator方法(只要引用的类型实际上有moveUp方法)。