java 单独类中的枚举

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5262442/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 10:18:24  来源:igfitidea点击:

enum in separate class

javaenums

提问by dsjoka

Is there anyway to put this test in a separate class? I tried but was unsuccessful.

反正有没有把这个测试放在一个单独的类中?我尝试过但没有成功。

public class TrafficLightprj 
{
    public enum TrafficLight 
    {  
        RED(1),  
        GREEN(2),  
        YELLOW(3);

        private final int duration; 

        TrafficLight(int duration) { 
            this.duration = duration; 
        }  

        public int getDuration() { 
            return this.duration; 
        } 


        public static void main(String[] args) 
        {
            for(TrafficLight light: TrafficLight.values())
            {
               System.out.println("The traffic light value is: " +light);
               System.out.println("The duration of that trafic light value is: " + light.getDuration());
            }
        }   
    }
}

回答by JasCav

I'm not sure I understand what you mean in your question, so I'll answer to what I thinkyou are asking.

我不确定我是否理解你在问题中的意思,所以我会回答我认为你在问什么。

An enum can be its own file in Java. For example, you could have a file called TrafficLightwhich, inside, is:

在 Java 中,枚举可以是它自己的文件。例如,您可以有一个名为TrafficLight的文件,它的内部是:

public enum TrafficLight {  
    RED(1),  
    GREEN(2),  
    YELLOW(3);

    private final int duration; 

    TrafficLight(int duration) { 
        this.duration = duration; 
    }  

    public int getDuration() { 
        return this.duration; 
    } 
}

You then can use this enum from your test project (TrafficLightPrj.java). Like so:

然后,您可以使用测试项目 ( TrafficLightPrj.java) 中的枚举。像这样:

public class TrafficLightprj {

    public static void main(String[] args) {
        for(TrafficLight light: TrafficLight.values()) {
            System.out.println("The traffic light value is: " +light);
            System.out.println("The duration of that trafic light value is: " +light.getDuration());
        }
    }
}

回答by adarshr

You need to put it in a different .java file itself. You can't have more than one public class / enum in a single .java file.

您需要将它放在不同的 .java 文件中。在一个 .java 文件中不能有多个公共类/枚举。

TrafficLightprj.java

TrafficLightprj.java

public class TrafficLightprj {

    public static void main(String[] args) {
        for(TrafficLight light: TrafficLight.values()){
            System.out.println("The traffic light value is: " +light);
            System.out.println("The duration of that trafic light value is: "  +light.getDuration());
        }
    }
}

TrafficLight.java

交通灯.java

public enum TrafficLight {  
    RED(1),  
    GREEN(2),  
    YELLOW(3);

    private final int duration; 

    TrafficLight(int duration) { 
        this.duration = duration; 
    }  

    public int getDuration() { 
        return this.duration; 
    }
} 

回答by Tom Hawtin - tackline

Sure, but you need to qualify the enum class name. TrafficLightprj.TrafficLightinstead of TrafficLight. Or, I guess, import it.

当然,但您需要限定枚举类名称。TrafficLightprj.TrafficLight而不是TrafficLight. 或者,我想,导入它。

I am not a big fan of non-private nested classes, and suggest making the enum top-level.

我不是非私有嵌套类的忠实粉丝,并建议将枚举设为顶级。

回答by Steven Jeuris

Assuming your question is: "How to get a nested enumin a different source file?"

假设您的问题是:“如何在不同的源文件中获取嵌套的枚举?”

This would be possible with partial classes, but this isn't supported in java.

这对于部分类是可能的,但在 java 中不受支持