工厂 Pattern/ArrayList/Interface 问题
Factory Pattern/ArrayList/Interface Issue
我正在使用工厂模式通过 ElevatorMover
接口创建不同类型的电梯。工厂将电梯存储在 ArrayList
个 ElevatorMover
对象中。当我从 ArrayList
检索电梯 [ElevatorArrayList.get(i)
] 时,我无法调用 PassengerElevator
方法。只有 ElevatorMover
方法(当然没有实现)。
我在这里做错了什么?
这是我试图在 main
中调用的代码
// 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);
}
当你调用接口方法时——实际上是任何方法(静态方法除外),被调用的方法是实际class中的方法(或作为尽可能接近它)- 不是引用类型中的那个。
考虑这段代码:
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
,那么无论是什么类型,它都会调用PassengerElevator
中的moveUp
方法引用是(只要引用的类型实际上有一个 moveUp
方法)。
我猜你没有创建 elevatorArrayList。示例
List<ElevatorMover> elevatorArrayList = new ArrayList<ElevatorMover>();
private ArrayList<ElevatorMover> elevatorArrayList; // This is just declaring not creating elevatorArrayList
我正在使用工厂模式通过 ElevatorMover
接口创建不同类型的电梯。工厂将电梯存储在 ArrayList
个 ElevatorMover
对象中。当我从 ArrayList
检索电梯 [ElevatorArrayList.get(i)
] 时,我无法调用 PassengerElevator
方法。只有 ElevatorMover
方法(当然没有实现)。
我在这里做错了什么?
这是我试图在 main
中调用的代码// 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);
}
当你调用接口方法时——实际上是任何方法(静态方法除外),被调用的方法是实际class中的方法(或作为尽可能接近它)- 不是引用类型中的那个。
考虑这段代码:
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
,那么无论是什么类型,它都会调用PassengerElevator
中的moveUp
方法引用是(只要引用的类型实际上有一个 moveUp
方法)。
我猜你没有创建 elevatorArrayList。示例
List<ElevatorMover> elevatorArrayList = new ArrayList<ElevatorMover>();
private ArrayList<ElevatorMover> elevatorArrayList; // This is just declaring not creating elevatorArrayList