Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-core/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 方法toString()问题_Java_Tostring - Fatal编程技术网

Java 方法toString()问题

Java 方法toString()问题,java,tostring,Java,Tostring,我正在创建食堂管理应用程序。我必须在某个地方使用重写的方法toString(),所以我把它放在类菜单中以显示菜单总价。但是我不能在main()中调用该方法。我该怎么做 class Menu { private Soup soup; private SecondMeal second; private Dessert desert; public Menu(int soupId, int secId, int desId) { soup = ne

我正在创建食堂管理应用程序。我必须在某个地方使用重写的方法toString(),所以我把它放在类菜单中以显示菜单总价。但是我不能在main()中调用该方法。我该怎么做

class Menu {

    private Soup soup;
    private SecondMeal second;
    private Dessert desert;

    public Menu(int soupId, int secId, int desId) {
        soup = new Soup(soupId);
        second = new SecondMeal(secId);
        desert = new Dessert(desId);
    }

    double price = soup.getPrice() + second.getPrice() + desert.getPrice();

    //here is my toString() method
    @Override
    public String toString() {
        String str = "Your menu price is " + price;
        return str;
    }

}

public class Testing {

    public static void main(String [] args) throws FileNotFoundException {

        Scanner scan = new Scanner(System.in);
        int soupId, secondId, desertId;

        do {
            System.out.println("Choose soup: ");
            Display.soupMenu();
            soupId = scan.nextInt();
        }
        while (soupId < 1 || soupId > 3);

        do {
            System.out.println("Choose second meal: ");
            Display.secondMenu();
            secondId = scan.nextInt();

        }
        while (secondId < 1 || secondId > 3);

        do {
            System.out.println("Choose dessert: ");
            Display.desertMenu();
            desertId = scan.nextInt();

        }
        while (desertId < 1 || desertId > 3);

        // here is the problem...
        System.out.println(Menu.toString()); // cannot make a static reference to the non-static method

    }
}
类菜单{
私家汤;
私人第二餐;
私人甜点沙漠;
公共菜单(int soupId、int secId、int desId){
汤=新汤(酸汤);
第二次=新的第二餐(secId);
沙漠=新甜点(desId);
}
双倍价格=soup.getPrice()+second.getPrice()+desert.getPrice();
//这是我的toString()方法
@凌驾
公共字符串toString(){
String str=“您的菜单价格为”+价格;
返回str;
}
}
公共类测试{
公共静态void main(字符串[]args)引发FileNotFoundException{
扫描仪扫描=新扫描仪(System.in);
int soupId、secondId、desertId;
做{
System.out.println(“选择汤:”;
Display.soupMenu();
soupId=scan.nextInt();
}
而(酸味<1 | |酸味>3);
做{
System.out.println(“选择第二顿饭:”);
Display.secondMenu();
secondId=scan.nextInt();
}
而(secondId<1 | | secondId>3);
做{
System.out.println(“选择甜点:”;
Display.menu();
沙漠ID=scan.nextInt();
}
而(沙漠ID<1 | |沙漠ID>3);
//问题是。。。
System.out.println(Menu.toString());//无法对非静态方法进行静态引用
}
}
建议:

  • 您需要在main方法中创建一个菜单实例,以便对其调用方法。我从那里开始
  • 您的其他类应该有
    toString()
    方法,包括所有的食物类
  • 在Menu的
    toString()
    方法中,您可以遍历所订购的食物,调用每个食物的toString()方法,以构建它将返回的菜单自己的字符串

System.out.println
toString不应手动调用。它将调用对象的实例。试着打电话

 System.out.println(new Menu()); //toString will automatically call here.

您必须创建Menu的一个实例并对其调用“toString())”,因为它不是静态的

注意这一行

double price = soup.getPrice() + second.getPrice() + desert.getPrice();
将立即失败。所有实例字段都在构造函数主体执行之前初始化。此时,
沙漠
。这将抛出一个
NullPointerException
。相反,在构造函数的末尾添加该行

double price;
...
public Menu(int soupId, int secId, int desId) {
    soup = new Soup(soupId);
    second = new SecondMeal(secId);
    desert = new Dessert(desId);
    price = soup.getPrice() + second.getPrice() + desert.getPrice();
}
有关如何在实例上调用
toString()
方法,请参阅其他答案。

  • 1:您不能在静态方法(如main方法)中调用非静态方法
  • 2:非静态类不能像--classname.method这样调用
  • 3您正在调用的应该是
    newmean(parameters)
    ,一个非静态类的对象

您已经告诉它如何将菜单对象转换为字符串;现在您需要创建一个菜单对象。您似乎对OOP不太熟悉,您可能需要刷新或阅读:)不,我对OOP不太感兴趣:)
new Menu(int,int,int)
您能给我举一个第三点的简短示例吗?我应该如何进行此迭代?@user2976091:不基于您当前的代码。但是假设所有的食物类都扩展了一个抽象的食物类父类。您的Menu类可以保存一个具体食物项目的
ArrayList
,在Menu的
toString()
中,您可以使用for循环遍历列表,将每个食物项目的
toString()
结果添加到方法返回的结果字符串中。