接口、抽象类和抽象类的方法



我正在学习如何使用工厂模式在Java中创建对象。我想创建类来管理汽车。汽车可以很小,也可以很大。我创建了一个接口,用于定义要由实现类实现的方法。抽象类实现了小型和大型汽车共享的接口的一些常用方法。具体的 SmallCar 和 LargeCar 类实现了抽象类的其余方法。

汽车界面

public interface Car {
 String getRegistrationNumber();
 void drive();
}

抽象汽车类实现汽车接口

public abstract class AbstractCar implements Car { 
 private final RegistrationNumber regNumber;
 private boolean tankFull = true;
 public AbstractCar(RegistrationNumber regNumber) {
  this.regNumber = regNumber;
 }
 @Override
 public final String getregistrationNumber() {
  return regNumber.toString();
 }
/**This method is not defined in the implemented Car interface. I added it to
*the abstract class because I want subclasses of these abstract class
*to have this method*/
 public boolean isTankFull() {
  return tankFull;
 }
}

小型车扩展抽象类

public final class SmallCar extends AbstractCar {
 public SmallCar(RegistrationNumber regNum) {
  super(regNum);
 }
 @Override
 public void drive() {
  //implemented here
 }
}

工厂等级

此类负责创建特定类型汽车的实例。

public final class CarFactory {
 public static Car createCar(String carType, RegistrationNumber regNum) {
  Car car = null;
  if (carType.equals("Small") {
   car = new SmallCar(regNum);
  }
  return car;                
}

主方法

RegistrationNumber regNum = new RegistrationNumber('a', 1234);
Car c = CarFactory.createCar("Small", regNum);
c.getRegistrationNumber(); //this works
c.isTankFull(); //this instance of Car cannot access the isTankFull method defined on the abstract class. The method is not defined on the Car interface though. I do not understand why.

挑战在于 Car 实例可以访问 Car 接口上定义的所有其他方法,但它无法访问在抽象类上定义但未在接口上定义的isTankFull()方法。我希望我的解释足够清楚。

你看不到该方法的原因是因为你的c对象被声明为Car接口。当然,当它来自您的工厂方法时,它是一个 SmallCar ,但您的变量只有接口。您可以将声明更改为 AbstractCar c = CarFactory.createCar("SmallCar", regnum);

在使用接口时

实现此目的的另一种方法是在尝试访问不在接口上的方法时将c对象强制转换为AbstractCar,但是您需要小心,因为您的工厂总是有可能返回实现Car的对象, 但不是AbstractCar.

if (c instanceof AbstractCar) {
    ((AbstarctCar)c).isTankFull();
}

当然,另一个简单的解决方案是将该方法添加到界面中,尽管这会从这个问题中删除教学机会。

好的解决方案是将您的isTankFull()放在界面上。这是有道理的,因为任何实施Car的汽车都需要访问isTankFull()

问题是:您是否正在创建任何无法isTankFull回答问题的Car?如果是这样,那么isTankFull移动到界面将没有意义。

另一种解决方案(如果您不希望isTankFull()位于接口上)是将Car转换为适当的类型:

if (c instanceof AbstractCar) {
    ((AbstractCar)c).isTankFull();
}

接口是您与实现它的类的用户创建的协定(或协议)。因此,您必须问自己是否有任何Car应该isTankFull公开信息(即应该响应消息isTankFull)。如果答案为"是",则必须将方法isTankFull添加到接口中。

查看您的代码,似乎类AbstractCar只是一个实用程序类。然后,isTankFull的方法应该被提升到接口,或者至少应该protected

另一方面,您必须问自己,您的客户端代码(即main方法)是否真的需要通用Car,或者是否需要特定类型的汽车,例如SmallCar

最后,考虑到接口的使用可以最大限度地减少组件之间的依赖关系。

最新更新