Java 为什么我收到错误“该类型的方法未定义”?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28660320/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why am I getting the error "the Method Is Undefined for the type"?
提问by Jon0149
I am learning the basics at University and would like some help with the following error from Eclipse : "The method getCost() is undefined for the type ShopCLI" &
我正在大学学习基础知识,并希望对 Eclipse 中的以下错误提供一些帮助:“该方法 getCost() 对于类型 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)
Here is my code
这是我的代码
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;
}
}
采纳答案by SMA
getCost
method is defined in order class and not in ShopCLI
class. So your code:
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());
Should be changed to
应该改为
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());
^^^^^
回答by Dogs
In this line:
在这一行:
System.out.println("This Will Cost " + getCost());
...you don't specify what you want to call getCost()
on, so it calls it on ShopCLI because that's where the call is happening. But ShopCLI doesn't have a getCost()
method. You need to call it on your order:
...您没有指定要调用的getCost()
内容,因此它在 ShopCLI 上调用它,因为那是调用发生的地方。但是 ShopCLI 没有getCost()
方法。您需要在订单上调用它:
System.out.println("This Will Cost " + ord.get(0).getCost());
This works, but when you create your Order object by calling the constructor, you're not calculating the cost, but rather setting whatever is passed in. Update your constructor to this:
这是可行的,但是当您通过调用构造函数创建 Order 对象时,您不是在计算成本,而是设置传入的任何内容。将构造函数更新为:
//Constructor
public Order(int OrderId, String outer, String inner, String sauce) {
super(outer, inner, sauce);
this.OrderId = OrderId;
double calcCost;
double outerCost = 0;
double innerCost = 0;
double sauceCost = 0;
//Outer Cost
if(outer == "Bun")
{
outerCost = 0.5;
}
else if(outer == "Bread")
{
outerCost = 0.25;
}
else if(outer == "Brioche")
{
outerCost = 0.75;
}
else
{
System.out.println("Invalid Bread Type");
}
//Inner cost
if(inner == "Ham")
{
innerCost = 0.5;
}
else if(inner == "Cheese")
{
innerCost = 0.25;
}
else if(inner == "Cucumber")
{
innerCost = 0.75;
}
else
{
System.out.println("Invalid Filling Type");
}
//Sauce Cost
if(sauce == "Mayo")
{
sauceCost = 0.5;
}
else if(sauce == "Butter")
{
sauceCost = 0.25;
}
else if(sauce == "Marmite")
{
sauceCost = 0.75;
}
else
{
System.out.println("Invalid Sauce Type");
}
calcCost = outerCost + innerCost + sauceCost;
this.Cost = calcCost;
}
Now your constructor works, but you will have to remove an argument where you're calling it, so change your ord.add line to this:
现在您的构造函数可以工作了,但是您必须删除调用它的参数,因此将 ord.add 行更改为:
ord.add(new Order(1, inputOuter, inputInner, inputSauce));
That should, if I'm not mistaken work. However, you might want to consider making a private helper method called calculateCost, so that you're not duplicating code between your constructor and your setOrder methods.
那应该,如果我没有弄错的话。但是,您可能需要考虑创建一个名为 calculateCost 的私有辅助方法,这样您就不会在构造函数和 setOrder 方法之间复制代码。
回答by cody b
you must get the object from the arraylist, then do acces the method:
您必须从数组列表中获取对象,然后访问该方法:
//get obj,then void
System.out.println("This Will Cost " + ord.get(choise).getCost());
this will return 0, since you set the cost 0 in the constructor:
这将返回 0,因为您在构造函数中设置了成本 0:
ord.add(new Order(1, inputOuter, inputInner, inputSauce, 0));
also, name your integer "Sandwich" to "sandwich" without the capital letter. otherwise, it looks like you mean the class "Sandwich"
此外,将您的整数“三明治”命名为“三明治”,不带大写字母。否则,看起来您的意思是“三明治”类
回答by AshanPerera
Create an Order
创建订单
Order order = new Order(1, inputOuter, inputInner, inputSauce, 0);
ord.add(order) // Add the order to your list.
The cost for an order is set by the method setOrder(int repOrderId, String repOuter, String repInner, String repSauce)
订单的成本由 setOrder(int repOrderId, String repOuter, String repInner, String repSauce) 方法设置
change this method to the following.
将此方法更改为以下内容。
public void setOrder() {
double calcCost;
double outerCost = 0;
double innerCost = 0;
double sauceCost = 0;
// Outer Cost
if (outer.equalsIgnoreCase("Bun")) {
outerCost = 0.5;
} else if (outer.equalsIgnoreCase("Bread")) {
outerCost = 0.25;
} else if (outer.equalsIgnoreCase("Brioche")) {
outerCost = 0.75;
} else {
System.out.println("Invalid Bread Type");
}
// Inner cost
if (inner.equalsIgnoreCase("Ham")) {
innerCost = 0.5;
} else if (inner.equalsIgnoreCase("Cheese")) {
innerCost = 0.25;
} else if (inner.equalsIgnoreCase("Cucumber")) {
innerCost = 0.75;
} else {
System.out.println("Invalid Filling Type");
}
// Sauce Cost
if (sauce.equalsIgnoreCase("Mayo")) {
sauceCost = 0.5;
} else if (sauce.equalsIgnoreCase("Butter")) {
sauceCost = 0.25;
} else if (sauce.equalsIgnoreCase("Marmite")) {
sauceCost = 0.75;
} else {
System.out.println("Invalid Sauce Type");
}
calcCost = outerCost + innerCost + sauceCost;
this.Cost = calcCost;
}
Now you can get the cost by calling the getCost() on the order as follows.
现在您可以通过调用订单上的 getCost() 来获取成本,如下所示。
To calculate the order cost, call order.setOrder(); To get the order cost, call order.getCost();
要计算订单成本,请调用 order.setOrder(); 要获取订单成本,请调用 order.getCost();
NOTE: Don't use == to compare string. Always use equals() or equalsIgnoreCase().
注意:不要使用 == 来比较字符串。始终使用 equals() 或 equalsIgnoreCase()。
回答by Anurag Bharti
i have modified your code for class ShopCLI
我已经修改了 ShopCLI 类的代码
package tester;
import java.util.ArrayList;
import java.util.Scanner;
public class ShopCLI {
public static void main(String[] args) {
ArrayList<Order> ord = new ArrayList<>();
int orderNumber =1;
System.out.println("Welcome to Sandwich Shop CLI V1!");
System.out.println("start order");
Scanner sc = new Scanner(System.in);
while(true){
System.out.println("Press 1 for new order or 2 to Exit");
int choice = sc.nextInt();
if (choice == 1){
System.out.println("Enter outer Options:Outer Options are Bun, Bread or Brioche");
String inputOuter = sc.next();
System.out.println("Enter inner Options:Inner Options are Ham, Cheese or Cucumber");
String inputInner = sc.next();
System.out.println("Enter sauce Options:Sauce Options are Mayo, Butter or Marmite");
String inputSauce = sc.next();
Order order1 = new Order(orderNumber, inputOuter, inputInner, inputSauce, 0);
order1.setOrder(inputOuter, inputInner, inputSauce);
System.out.println("You Made a " + inputInner + " with " + inputSauce + " Sandwich on " + inputOuter);
System.out.println("This Will Cost " + order1.getCost());
ord.add(order1);
}
else if (choice == 2){
System.out.println("Exited.");
System.exit(1);
}
}
}
}
and format of setOrder
method is modified too
public void setOrder(String repOuter, String repInner, String repSauce)
并且setOrder
方法的格式也被修改了
public void setOrder(String repOuter, String repInner, String repSauce)