void在void中做了什么? [英] What does void do in java?

查看:163
本文介绍了void在void中做了什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


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

The return type—the data type of the value returned by the method, or void if the method does not return a value.

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

好的,那么..这是我的问题:

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


    }
}

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

我错过了什么?
为什么那里有空白?
它确实返回一个字符串?

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:

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

为了进一步解决这个问题,println是一个方法的典型例子,它以副作用。

To press the issue further, println is a classic example of a method which is performing computation as a "side effect."

这篇关于void在void中做了什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆