如何从继承类的父级的父级中调用方法? [英] How do I call a method from a parent of a parent of an inherited class?

查看:238
本文介绍了如何从继承类的父级的父级中调用方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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值,这就是这里的问题.我不知道为什么这些值不存在.

I realize that t.items has 0 values which is the problem here. What I do not know is why these values are not there.

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

If anyone wants to see the files: Commodity.java https://pastebin.com/raw/9sWbDWV8

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

TimsProduct.java extends commodity https://pastebin.com/raw/jzgfkd0P

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

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

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

Donut.java extends TimsProduct https://pastebin.com/raw/w7iEQG1H

推荐答案

这是基于OP提供的内容的初步响应.

This is an initial response based on what little the OP provided.

您不会在派生类中覆盖获取器(并且基本的基本实现无法编译):

You are not overriding your getters in your deriving classes (and the basic implementation in the base can not compile) :

public abstract class Commodity {

    public double getProductionCost() {
       // no return value!
    }

    public double getRetailPrice() {
       // no return value!         
    }
}

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


    public TimsProduct(String name, double cost, double price){
        this.name = name;
        this.cost = cost;
        this.price = price;

    } 

    public String getName(){
        return name;
    }
    // no @Override!
    public double getProductionCost(){
        return cost;
}
    // no @Override!
    public double getRetailPrice(){
        return price;
    }

    public String toString(){
        return ("Name is:  " + name + "cost is: " + cost + "price is: " + price );
    }
}

这篇关于如何从继承类的父级的父级中调用方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆