void在Java中做什么?

返回类型-方法返回的值的数据类型;如果该方法未返回值,则返回void

http://download.oracle.com/javase/tutorial/java/javaOO/methods.html

那好吧..这是我的问题:

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();

}

}

http://download.oracle.com/javase/tutorial/java/javaOO/enum.html

上面的代码没有无效就无法工作。

Exception in thread "main" java.lang.Error: Unresolved compilation problems: 

The method tellItLikeItIs() is undefined for the type EnumTest

我错过了什么?为什么那里有空隙?并返回字符串吗?

回答:

代码无法运行的原因void是因为该System.out.println(String

string)方法不返回任何内容,仅将提供的参数打印到标准输出终端(在大多数情况下为计算机监视器)。当方法返回“

nothing”时,您必须通过将void关键字置于其签名中来进行指定。

您可以在此处查看System.out.println的文档:

http://download.oracle.com/javase/6/docs/api/java/io/PrintStream.html#println%28java.lang.String%29

为了进一步解决这个问题,println是将计算作为“副作用”执行的方法的经典示例。

以上是 void在Java中做什么? 的全部内容, 来源链接: utcz.com/qa/417818.html

回到顶部