java - 为什么我收到错误 "the Method Is Undefined for the type"?

标签 java methods types constructor undefined

我正在大学学习基础知识,希望获得有关 Eclipse 中以下错误的帮助:“The method getCost() is undefined for the type ShopCLI”&

"Exception in thread "main" java.lang.Error: Unresolved compilation problem:
    The method getCost() is undefined for the type ShopCLI
    at components.ShopCLI.main(ShopCLI.java:39)

这是我的代码

public class ShopCLI {

    public static void main(String[] args) {

        ArrayList<Order> ord = new ArrayList<>();

        System.out.println("Welcome to Sandwich Shop CLI V1!");
        System.out.println("Please Choose and Option by Typing the Appropriate Number from the List");
        System.out.println("1.New Order");

        Scanner sc = new Scanner(System.in);
        int choice = sc.nextInt();

        System.out.println("Please Choose an Outer From the List: ");
        System.out.println("Press 1 to Continue or 2 to Exit");
        int Sandwich = sc.nextInt();

        System.out.println("Outer Options are Bun, Bread or Brioche");

        String inputOuter = sc.next();

        System.out.println("Inner Options are Ham, Cheese or Cucumber");

        String inputInner = sc.next();

        System.out.println("Sauce Options are Mayo, Butter or Marmite");

        String inputSauce = sc.next();


        if (Sandwich == 1){
            ord.add(new Order(1, inputOuter, inputInner, inputSauce, 0));
            System.out.println("You Made a " + inputInner + " with " + inputSauce + " Sandwich on " + inputOuter);
            System.out.println("This Will Cost " + getCost());
        }
        else if (Sandwich == 2){
            System.out.println("Exited.");
        }

    }

}

public class Sandwich {

    //Fields
    ArrayList<Sandwich> sandwich = new ArrayList<>();
    String outer;
    String inner;
    String sauce;

    //Constructor
    public Sandwich(String outer, String inner, String sauce){
        this.outer = outer;
        this.inner = inner;
        this.sauce = sauce;
    }

    //Methods

    public String getOuter(){
        return outer;
    }

    public String getInner(){
        return inner;
    }

    public String getSauce(){
        return sauce;
    }

    public void setOuter(String repOuter){
        outer = repOuter;
    }

    public void setInner(String repInner){
        inner = repInner;
    }

    public void setSauce(String repSauce){
        sauce = repSauce;
    }

    public void createSandwich(String outer, String inner, String sauce){
        sandwich.add(new Sandwich(outer, inner, sauce));
    }

    public void setSandwich(String repOuter, String repInner, String repSauce){
        outer = repOuter;
        inner = repInner;
        sauce = repSauce;
    }

    public void resetOrder(){
        sandwich.removeAll(sandwich);
    }
}

public class Order extends Sandwich {

    //Fields
    int OrderId;
    double Cost;


    //Constructor
    public Order(int OrderId, String outer, String inner, String sauce, int Cost) {
        super(outer, inner, sauce);
        this.OrderId = OrderId;
        this.Cost = Cost;

    }

    //Methods

    public int getOrderId(){
        return this.OrderId;
    }

    public double getCost(){
        return this.Cost;
    }

    public void setOrderId(int repOrderID){
        this.OrderId = repOrderID;
    }

    public void overrideCost(int Cost){
        this.Cost = Cost;
    }

    public void setOrder(int repOrderId, String repOuter, String repInner, String repSauce){
        this.OrderId = repOrderId;
        this.outer = repOuter;
        this.inner = repInner;
        this.sauce = repSauce;

        double calcCost;
        double outerCost = 0;
        double innerCost = 0;
        double sauceCost = 0;

        //Outer Cost
        if(repOuter == "Bun")
        {
            outerCost = 0.5;
        }
        else if(repOuter == "Bread")
        {
            outerCost = 0.25;
        }
        else if(repOuter == "Brioche")
        {
            outerCost = 0.75;
        }
        else
        {
            System.out.println("Invalid Bread Type");
        }

        //Inner cost
        if(repInner == "Ham")
        {
            innerCost = 0.5;
        }
        else if(repInner == "Cheese")
        {
            innerCost = 0.25;
        }
        else if(repInner == "Cucumber")
        {
            innerCost = 0.75;
        }
        else
        {
            System.out.println("Invalid Filling Type");
        }

        //Sauce Cost
        if(repSauce == "Mayo")
        {
            sauceCost = 0.5;
        }
        else if(repSauce == "Butter")
        {
            sauceCost = 0.25;
        }
        else if(repSauce == "Marmite")
        {
            sauceCost = 0.75;
        }
        else
        {
            System.out.println("Invalid Sauce Type");
        }

        calcCost = outerCost + innerCost + sauceCost;
        this.Cost = calcCost;

    }

}

最佳答案

getCost 方法是在订单类中定义的,而不是在 ShopCLI 类中定义的。所以你的代码:

ord.add(new Order(1, inputOuter, inputInner, inputSauce, 0));
System.out.println("You Made a " + inputInner + " with " + inputSauce + " Sandwich on " + inputOuter);
System.out.println("This Will Cost " + getCost());

应改为

Order order = new Order(1, inputOuter, inputInner, inputSauce, 0);
ord.add(order);
System.out.println("You Made a " + inputInner + " with " + inputSauce + " Sandwich on " + inputOuter);
System.out.println("This Will Cost " + order.getCost());
                                       ^^^^^

关于java - 为什么我收到错误 "the Method Is Undefined for the type"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28660320/

相关文章:

java - 使用枚举访问静态变量

c# - 在 C# 中的表单中共享实例和方法/变量

c++ - 为什么以及何时执行重载构造函数?

c# - 用一种方法改变 label.visibility - 为什么它不隐藏?

haskell - 在 Haskell 中打印我自己的数据类型的自定义表示

c++ - C++ 中软件版本的正确数据类型是什么?

python - (Python) 使用 numpy.genfromtxt 用数据填充列表(不同数据类型)

java - 防止 Wicket 8 AjaxEventBehavior onChange 为不在子页​​面上提交表单

java - firebase 数据库到自定义 ListView 问题

java - 首选 toString() 实现?