Java 类型为 return 的继承方法

Java inherance method with return type

我的 class 这样写对吗?有问题的是 Item class 中的方法 getPrice()。每个项目都需要有一个 getPrice()。但我实际上不能 return 一些东西。所以我开火 this.getPrice() 得到 ProductItem 的价格。是否有更可靠/设计更好的解决方案?

class Item {
    String description;

    public Item(String description) {
        this.description = description;
    }

    double getPrice(){return this.getPrice();} //TODO Correct like this?
}

class ProductItem extends Item {
    int amount;
    double pricePerUnit;

    public ProductItem(String description, int amount, double pricePerUnit)         {
        super(description);
        this.amount = amount;
        this.pricePerUnit = pricePerUnit;
    }

    @Override
    double getPrice(){
        return amount * pricePerUnit;
    }
}

听起来 Item 应该是一个抽象 class 然后,getPrice() 是一个抽象方法:

public abstract class Item {
    private final String description;

    public Item(String description) {
        this.description = description;
    }

    public abstract double getPrice();

    public String getDescription() {
        return description;
    }
}

这意味着你不会写

Item item = new Item("foo"); // Invalid, because Item is abstract

但是你可以这样写:

Item item = new ProductItem("foo", 10, 2.0);
double p = item.getPrice(); // 20.0

您声明的每个具体(非抽象)子class 都必须覆盖getPrice() 并提供实现。

有关详细信息,请参阅 abstract classes and methods section of the Java tutorial

以防万一你错过了它,class Item 有问题,其中方法 getPrice() 是递归的。所以这里 new Item().getPrice() 将导致 WhosebugException。

你可以把 class 写成抽象的。

abstract class Item {
  String description;
  public Item(String description) {
    this.description = description;
  }
  abstract double getPrice(); // force child classes to override this method
}

你要做的就是让你的 Item class abstract,通过抽象化 getPrice 方法。

这会强制您的子classes 实现特定功能,否则编译器会报错。

您可以按如下方式进行:

class Item {
    String description;

    public Item(String description) {
        this.description = description;
    }

    abstract double getPrice();
}

当子class忘记实现功能时,您提供的实现实际上会导致对自身的循环(无限循环)调用。当 subclass 实现了函数时,它根本不会被调用,因为它被 subclass.

覆盖了

为什么不能制作getPrice()Itemclassabstract,如下:

  public abstract class Item {
    private String description;

    public Item(final String description) {
        this.description = description;
    }

    protected abstract double getPrice();

    public String getDescription() {
        return description;
    }
}

并在class中提供实现,扩展如下:

public class ProductItem extends Item {
    private int amount;
    private double pricePerUnit;

    public ProductItem(final String description, final int amount, final double pricePerUnit) {
        super(description);
        this.amount = amount;
        this.pricePerUnit = pricePerUnit;
    }

    @Override
    protected double getPrice() {
        return amount * pricePerUnit;
    }

}

double getPrice(){return this.getPrice();} 基本上是一个无限循环。 你应该先设定那个价格。

class Item {
    String description;
    double price;
    public Item(String description) {
        this.description = description;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    double getPrice(){return this.price;} 
}