我有一个父类称为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
?
希望我在我的问题中很清楚。
“这是做这件事的正确方法吗?” - 做什么?遗产?你做到了。 “我怎样才能从食物中调用我的变量'DELIVERY'” - 只是使用它。它是在那里定义的,所以没有问题来提及它。 – AlexR
@AlexR:您如何从父母处访问子字段? – mok
我是否必须以某种方式在父类中指定一个变量以用于交付? – ddvink