【发布时间】:2016-07-29 07:18:31
【问题描述】:
我有一个产品。我使用的是以前的课程,因为以前和现在的唯一区别是,如果订单数量超过 10 个单位,可以选择乘以价格的 10% 折扣(浮动 0.1)。
我使用 DisountedProduct 作为继承类扩展 Product 类,但是当我从父类调用构造函数时,我收到错误“方法声明无效,需要返回类型。”
我已经阅读了java对继承的解释,但没有提到这一点。
请帮忙
public class DiscountedProduct extends Product {
private float discount;
private String name;
private double price;
private int quantity;
public Product(String name, double price, int quantity){
this.name = name;
this.price = price;
this.quantity = quantity;
}
//Get//
public float getDiscount(){
return discount;
}
//Set//
public void setDiscount(float value){
this.discount = value;
}
}
public class Product {
private String name;
private double price;
private int quantity;
public Product(String name, double price, int quantity){
this.name = name;
this.price = price;
this.quantity = quantity;
}
//Get Methods//
public String getName(){
return name;
}
public double getPrice(){
return price;
}
public int getQuantity(){
return quantity;
}
public double getTotalPrice(){
return quantity * price;
}
//Set Methods//
public void setName(String value){
this.name = value;
}
public void setPrice(double value){
this.price = value;
}
public void setQuantity(int value){
this.quantity = value;
}
}
【问题讨论】:
标签: java inheritance