1

我尝试println使用我为超类创建的对象从超类中调用方法。我收到了这个错误

“运算符 + 不能应用于 java.lang.string void”

System.out.println("Contents of objsuper: " + objsuper.showij());
4

3 回答 3

1

如果您查看该方法objsuper.showij(),您会发现它什么也不返回void

这不能附加到某些东西上

于 2019-10-28T06:50:48.450 回答
1

替换以下行

System.out.println("Contents of objsuper: " + objsuper.showij());

System.out.println("Contents of objsuper: ");
objsuper.showij();

原因是 is 的返回类型,objsuper.showij()因此void不能operator +应用于它。方法void就像做某事但不返回值,由于它没有返回值,因此您不能使用+运算符将​​其附加到任何内容;您需要按照我上面所做的方式单独调用它。

于 2019-10-28T06:59:04.937 回答
0

在 Syso 中,'+' 是字符串文字连接的一种方式。您可以使用带有 toString() 方法的字符串或对象。由于 void 函数不返回任何内容,因此您不能在 System.out.println 中使用它

于 2019-10-28T06:54:31.310 回答