问题描述
好的,那么..这是我的问题:
Okay, then.. Here is my question:
public class EnumTest {
Day day;
public EnumTest(Day day) {
this.day = day;
}
public void tellItLikeItIs() {
switch (day) {
case MONDAY: System.out.println("Mondays are bad.");
break;
case FRIDAY: System.out.println("Fridays are better.");
break;
case SATURDAY:
case SUNDAY: System.out.println("Weekends are best.");
break;
default: System.out.println("Midweek days are so-so.");
break;
}
}
public static void main(String[] args) {
EnumTest firstDay = new EnumTest(Day.MONDAY);
firstDay.tellItLikeItIs();
EnumTest thirdDay = new EnumTest(Day.WEDNESDAY);
thirdDay.tellItLikeItIs();
EnumTest fifthDay = new EnumTest(Day.FRIDAY);
fifthDay.tellItLikeItIs();
EnumTest sixthDay = new EnumTest(Day.SATURDAY);
sixthDay.tellItLikeItIs();
EnumTest seventhDay = new EnumTest(Day.SUNDAY);
seventhDay.tellItLikeItIs();
}
}
上述代码无效无效。
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
The method tellItLikeItIs() is undefined for the type EnumTest
我错过了什么?
为什么那里有空白?
它确实返回一个字符串?
What did I miss out?Why is there a void in there?And it does return a string?
推荐答案
没有代码无法运行的原因void
是因为 System.out.println(String string)
方法没有返回任何内容,只是将提供的参数打印到标准输出终端,这是大多数情况下是电脑显示器。当方法返回无时,您必须通过在其签名中放置 void
关键字来指定。
The reason the code will not work without void
is because the System.out.println(String string)
method returns nothing and just prints the supplied arguments to the standard out terminal, which is the computer monitor in most cases. When a method returns "nothing" you have to specify that by putting the void
keyword in its signature.
您可以在这里看到System.out.println的文档:
You can see the documentation of the System.out.println here:
为了进一步解决这个问题,println是一个方法的典型例子,它以副作用。
To press the issue further, println is a classic example of a method which is performing computation as a "side effect."
这篇关于void在void中做了什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!