【问题标题】:How do I call a method from a parent of a parent of an inherited class? [closed]如何从继承类的父级的父级调用方法? [关闭]
【发布时间】:2018-09-17 12:02:50
【问题描述】:
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 值,这是这里的问题。我不知道为什么这些值不存在。

如果有人想查看文件: 商品.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

【问题讨论】:

  • 相关代码必须在问题本身中。不在pastebin。
  • 什么是物品类型?找不到任何产品类型。你是说 TimsProduct 吗?
  • 抱歉我已经更新了。我的意思是 TimsProduct。我正在尝试用断点调试它
  • 请务必遵循@JBNizet 的建议——所有代码都必须在您的问题中以代码格式文本的形式出现。请理解,这与其说是一个帮助网站,不如说是一个问答网站,其中的问答应该对所有人都有帮助,而不仅仅是原始海报。
  • 请查看minimal reproducible example 链接。它会告诉你我们需要你发布什么代码。

标签: java inheritance


【解决方案1】:

这是基于 OP 提供的少量内容的初始响应。

您没有在派生类中覆盖您的 getter(并且基础中的基本实现无法编译):

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 );
    }
}

【讨论】:

  • 作为重要的旁注 - 其余代码是工厂 DP、UI 输入处理代码和业务逻辑代码的混杂。我强烈建议审查并重新设计此代码。
  • 感谢您的回复。我试图将覆盖添加到建议的函数中,但它仍然不起作用。 TimsProduct 是一个空数组,所以我正在尝试调试它。干杯
  • 究竟是什么不起作用?我们需要更多信息来帮助你,伙计....
  • Rann,我们试图哄骗@user2372210 来改善他的问题,但无济于事。 :(
  • 道歉,但我正在重写它..
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-05-16
  • 2017-08-04
  • 1970-01-01
相关资源
最近更新 更多