【问题标题】:Inheritance with static values具有静态值的继承
【发布时间】:2014-03-02 09:47:53
【问题描述】:

我有一个名为Product 的父类和一个名为Food 的子类。每个Product 都有一个交货时间。例如Food 是一天(我将其定义为一天的int)。

我的Product 课程是这样的:

public abstract class Product {    
    private int id;
    private String description;
    private double price;

    public Product(int id, String description, double price) {
        this.id = id;
        this.description = description;
        this.price = price;
    }.... and so on

我的Food 类如下所示:

public class Food extends Product{

    private Date expirationDate;
    private static final int DELIVERY = 1;

    public Food(int id, String description, double price, Date expirationDate) {
        super(id, description, price);
        this.expirationDate = expirationDate;
    }.. and so on

这是正确的做法吗?其次,如何从Food 调用我的变量DELIVERY

希望我的问题很清楚。

【问题讨论】:

  • "这是正确的做法吗?" - 做什么?遗产?你做到了。 “我怎样才能从 Food 中调用我的变量 'DELIVERY'” - 只需使用它。它是在那里定义的,所以引用它没有问题。
  • @AlexR:如何从父级访问子字段?
  • 是否必须以一种或另一种方式在父类中指定一个变量才能交付?
  • 成员变量不能像方法一样被覆盖。无论如何,如果您想在两个类(父类和子类)中使用一个变量,请在父类(受保护)中定义它,否则在子类中定义它。

标签: java inheritance static final extends


【解决方案1】:

每个产品都有交货期

我猜您希望能够从外部访问任何产品的这些信息。所以你的 Product 类必须有如下方法:

/**
 * Returns the delivery time for this product, in days
 */
public int getDeliveryTime()

现在你不得不怀疑了。交货时间是每个产品的固定值,可以在施工时计算,之后永远不会改变,或者交货时间是从产品的其他字段计算出来的,还是服从公式。第一种情况,交货时间可以是Product类的一个字段,在构造函数中初始化:

private int deliveryTime;

protected Product(int id, String description, double price, int deliveryTime) {
    this.id = id;
    this.description = description;
    this.price = price;
    this.deliveryTime = deliveryTime;
}

/**
 * Returns the delivery time for this product, in days
 */
public int getDeliveryTime() {
    return deliveryTime;
}

在第二种情况下,(这似乎是您的情况),您应该让每个子类根据需要计算交货时间:

/**
 * Returns the delivery time for this product, in days
 */
public abstract int getDeliveryTime();

在食品中,例如:

@Override
public int getDeliveryTime() {
    return 1; // always 1 for Food. Simplest formula ever
}

很酷的是 Product 类和子类的用户不需要关心它是如何实现的。他们只知道每个Product 都有一个getDeliveryTime() 方法。它的实现方式与它们无关,并且可以在不更改调用者代码中的任何内容的情况下进行更改。这就是封装的美妙之处。

【讨论】:

    【解决方案2】:

    如果每个产品都有交货时间,那么最好放在基类中。

    public abstract class Product {
    
    private int id;
    private String description;
    private double price;
    
    protected final int deliveryTime;
    
    public Product(int id, String description, double price, int deliveryTime) {
        this.id = id;
        this.description = description;
        this.price = price;
        this.deliveryTime = deliveryTime;
    }
    

    public class Food extends Product{
    public Food(int id, String description, double price, Date expirationDate) {
        super(id, description, price, 1);
        this.expirationDate = expirationDate;
    }
    //...
    }
    

    我在母类中设置了 DELIVERY 保护,但您不妨将其设为私有并拥有一个 setter/getter(不过,仅当您希望该字段可从代码的其他部分访问时)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-05-28
      • 1970-01-01
      • 1970-01-01
      • 2017-02-07
      • 2020-06-03
      • 1970-01-01
      • 2012-01-16
      • 1970-01-01
      相关资源
      最近更新 更多