我有两个接口和两个类,Order类是CoffeeBagOrder的父级,没有编译器错误,只是没有显示,我看不到为什么
订单类:
dd(session()->all())
CoffeeBagOrder类
public abstract class Order implements OrderInterface {
//variables
final static double SALES_TAX = 0.1; //not initialised by constructor
int unitWeight, numberOfUnits;
public Order() {
unitWeight=0;
numberOfUnits=0;
}
public Order(int unitWeight, int numberOfUnits) {
unitWeight=unitWeight;
numberOfUnits=numberOfUnits;
}
public void numberOfItems(int number) {
numberOfUnits=number;
}
public void unitWeight(int weight) {
unitWeight=weight;
}
}
主要
public class CoffeeBagOrder extends Order implements Sales {
final static double PRICE_PER_KG = 5.55;
double salesBeforeTax;
double tax;
double totalSales;
public CoffeeBagOrder() {
}
public CoffeeBagOrder(int unitWeight, int numberOfUnits) {
super(unitWeight,numberOfUnits);
}
public double calculateSalesBeforeTax() {
salesBeforeTax= unitWeight*numberOfUnits*5.50;
return salesBeforeTax;
}
public double calculateSalesTax() {
tax=salesBeforeTax*0.10;
return tax;
}
public double calculateTotalSales() {
totalSales=salesBeforeTax+tax;
return totalSales;
}
//Override
public String toString() {
return "Price before tax: "+calculateSalesBeforeTax()+"\nTax: "+calculateSalesTax()+"\nTotal price: "+calculateTotalSales();
}
}
我已经省略了接口,但是相应地遵循了它们,在此先感谢您,我也不确定我是否有效地编写了构造函数,因为它们都是相同的?
答案 0 :(得分:0)
按如下所示更改您的Order类构造函数(选中此复选框):
public Order(int unitWeight, int numberOfUnits) {
this.unitWeight=unitWeight;
this.numberOfUnits=numberOfUnits;
}
您没有在构造函数中更新类字段!它正在为其分配参数。
并按如下所示修改CoffeeBagOrder中的toString(检查注释):
@Override
public String toString() {
return "Price before tax: "+calculateSalesBeforeTax()+"\nTax: "+calculateSalesTax()+"\nTotal price: "+calculateTotalSales();
}
当您打算覆盖时,始终使用@Override注释以使其生效,这是最佳实践,而不是显式调用toString()。
并使用
打印System.out.println(customer);