为什么无法在system.out.println中调用void方法

时间:2017-08-14 14:43:46

标签: java

有人可以回答为什么这段代码会出错吗?

package hello;

public class Hello {

    public void eat() {
        System.out.println("eating");
    }

    private String run() {
    return "dwedsdfsdfsdf fsdf rgdsfG";

    }

    public static void main(String[] args) {
        //System.out.println("Hello bhopi");
        //Hello hello = new Hello();
        Hello mahir = new Hello();
        //String y = mahir.eat();

        System.out.println(mahir.run());
        System.out.println(mahir.eat());
    }
}

2 个答案:

答案 0 :(得分:1)

因为x coordinate of origin方法没有返回任何内容,所以没有任何内容可以打印。方法extreme left期望将Object作为参数进行打印。

答案 1 :(得分:1)

1)没有方法可以接受对void方法的调用作为参数 就像你将void参数传递给方法一样。

2)此处println()引用PrintStream.println()方法,因为out字段被声明为PrintStream
 要在调用它时编译正常,您必须指定一个与此方法的重载版本之一匹配的参数。

相关问题