我尝试println
使用我为超类创建的对象从超类中调用方法。我收到了这个错误
“运算符 + 不能应用于 java.lang.string void”
System.out.println("Contents of objsuper: " + objsuper.showij());
我尝试println
使用我为超类创建的对象从超类中调用方法。我收到了这个错误
“运算符 + 不能应用于 java.lang.string void”
System.out.println("Contents of objsuper: " + objsuper.showij());
如果您查看该方法objsuper.showij()
,您会发现它什么也不返回void
这不能附加到某些东西上
替换以下行
System.out.println("Contents of objsuper: " + objsuper.showij());
和
System.out.println("Contents of objsuper: ");
objsuper.showij();
原因是 is 的返回类型,objsuper.showij()
因此void
不能operator +
应用于它。方法void
就像做某事但不返回值,由于它没有返回值,因此您不能使用+
运算符将其附加到任何内容;您需要按照我上面所做的方式单独调用它。
在 Syso 中,'+' 是字符串文字连接的一种方式。您可以使用带有 toString() 方法的字符串或对象。由于 void 函数不返回任何内容,因此您不能在 System.out.println 中使用它