2015-02-10 64 views
0

我正在使用工厂模式创建使用ElevatorMover接口的不同类型的电梯。工厂将电梯存放在ElevatorMover对象的ArrayList中。当我从ArrayList检索电梯[ElevatorArrayList.get(i)]时,我无法拨打PassengerElevator方法。只有ElevatorMover方法(当然没有执行)。工厂模式/ ArrayList /接口问题

我在这里没有做什么?

这里是我试图在主

// 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); 
} 
+0

代码段请。 – Kode 2015-02-10 01:50:25

+0

为什么您认为PassengerElevator方法在该方法可能返回ElevatorMover时应该可用? – 2015-02-10 01:52:23

+0

@AmirAfghani也许我误解了接口的功能,但我使用的接口是A)在不同类型的电梯之间提供通用方法,B)允许将我的各种类型的电梯分组到ArrayList中。 – clenard 2015-02-10 02:00:20

回答

1

我猜你没有创建elevatorArrayList。例如

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

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

的问题。我会在上面张贴它。 – clenard 2015-02-10 02:41:22

+1

您刚刚声明了变量elevatorArrayList。你必须使用上面提到的新操作符创建一个ArrayList – Kode 2015-02-10 02:43:40

+0

@Vmin先生或先生,你太棒了。你可以肯定,这将是我将永远带在身边的一个教训,所以这不会再发生。非常感谢你! – clenard 2015-02-10 02:47:27

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方法)。

+0

这不是问题的答案。 – Kode 2015-02-10 02:23:00

+0

@Vwin怎么样?我将这个问题理解为“这不会起作用,因为'ElevatorMover.moveUp'被调用,而不是'PassengerElevator.moveUp',对吗?” – immibis 2015-02-10 02:26:15

+0

@Vmin感谢您的支持!这确实支持我理解接口和覆盖方法。所以,至少我知道我的方法调用可能不是问题。这似乎是我填写我的ArrayList – clenard 2015-02-10 02:33:47