如何从继承的类中调用方法?

时间:2018-04-07 20:54:38

标签: java inheritance dependencies

public class TimsOrder {

    private int size;
    private String name;
    private static TimsProduct[] items;

    private TimsOrder(String name, int size) {
        this.name = name;
        this.size = size;
    }

    @Override
    public double getRetailPrice(){
        return price;
    }

    private static void orderItem(TimsProduct item) {
        Donut chocolate = Donut.create();
        item = chocolate;
    }

    public static TimsOrder create() {
        items = new TimsProduct[size];
        for (int i = 0; i < items.length; i++) {
            orderItem(items[i]);
        }
        TimsOrder order = new TimsOrder("OrderName", 1); //Where 1 is the # of items
    }

    public double getAmountDue() {
        double total = 0;
        System.out.println("Testpoint");
        for (int i = 0; i < items.length; i++) {
            total = total + (((TimsProduct) items[i]).getRetailPrice()); //Line with issue
        }
        return total;
    }
}

public abstract class TimsProduct extends Commodity {
    private String name;
    private double cost;
    private double price;

    @Override
    public double getRetailPrice(){
        System.out.println("Testpoint2");
        return price;
    }
}

public class Donut extends TimsProduct {
    private String description;
    private int calorieCount;

    private Donut(String name, String description, double cost, double price, int calorieCount) {
        super(name, cost, price);
        this.description = description;
        this.calorieCount = calorieCount;
    }

    public static Donut create() {
        Donut chocolate = new Donut("Chocolate", "Glazed", 0.30, 0.99, 500);
        return chocolate;
    }
}

测试代码:

TimsOrder t = TimsOrder.create();
System.out.println(t);
System.out.printf("Total Price: $%.2f\n", t.getAmountDue());

我意识到t.items有0个值,这就是问题所在。我不知道为什么这些价值观不存在。

如果有人想查看文件: Commodity.java https://pastebin.com/raw/9sWbDWV8

TimsProduct.java扩展商品 https://pastebin.com/raw/jzgfkd0P

TimsOrder.java https://pastebin.com/raw/vc0VtDq6

Donut.java扩展了TimsProduct https://pastebin.com/raw/w7iEQG1H

0 个答案:

没有答案