单独类中的枚举

发布于 2024-10-21 06:36:58 字数 748 浏览 1 评论 0原文

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

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

老旧海报 2024-10-28 06:36:58

我不确定我是否理解您的问题的意思,因此我将回答我认为您所问的问题。

在 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; 
    } 
}

然后您可以在测试项目 (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());
        }
    }
}

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

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

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:

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());
        }
    }
}
燃情 2024-10-28 06:36:58

您需要将其本身放入不同的 .java 文件中。在一个 .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

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 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.

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

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; 
    }
} 
£烟消云散 2024-10-28 06:36:58

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

我不太喜欢非私有嵌套类,并建议将枚举设为顶级。

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

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

被你宠の有点坏 2024-10-28 06:36:58

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

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

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文