为什么我可以将其他类的常数添加到Java中的Switch语句中?

发布于 2025-01-21 21:54:06 字数 1593 浏览 0 评论 0原文

为什么我不能将其他类的常数添加到Java中的Switch语句中?

示例:我有一个课程

public class Game {


    static class GameMode {
        public static final GameMode SURVIVAL = new GameMode();
        public static final GameMode ADVENTURE = new GameMode();
        public static final GameMode GOD = new GameMode();
    }

    GameMode CurrentMode = GameMode.GOD;

}

,但是当我定义一个开关语句时,它给了我一个错误:

    void OnGame(){
        switch (CurrentMode){
            case GameMode.GOD: // case expressions must be constant expressionsJava(536871065)
                System.out.println("Player On God Mode");
                break;
            case GameMode.SURVIVAL: // case expressions must be constant expressionsJava(536871065)
                System.out.println("Player On Survival Mode");
                break;
            case GameMode.ADVENTURE: // case expressions must be constant expressionsJava(536871065)
                System.out.println("Player On Adventure Mode");
                break;


        }
    }

这让我感到困惑。生存,冒险和上帝模式都是恒定的,我在它面前添加了“最终”,为什么我不能在开关语句中使用它?

这是一些图像:

Why can't I add constant of other class into a switch statement in java?

example: I have a class

public class Game {


    static class GameMode {
        public static final GameMode SURVIVAL = new GameMode();
        public static final GameMode ADVENTURE = new GameMode();
        public static final GameMode GOD = new GameMode();
    }

    GameMode CurrentMode = GameMode.GOD;

}

but when I define a switch statement, It give me an error:

    void OnGame(){
        switch (CurrentMode){
            case GameMode.GOD: // case expressions must be constant expressionsJava(536871065)
                System.out.println("Player On God Mode");
                break;
            case GameMode.SURVIVAL: // case expressions must be constant expressionsJava(536871065)
                System.out.println("Player On Survival Mode");
                break;
            case GameMode.ADVENTURE: // case expressions must be constant expressionsJava(536871065)
                System.out.println("Player On Adventure Mode");
                break;


        }
    }

That makes me confused. SURVIVAL, ADVENTURE and GOD mode are all constant, I add "final" in front of it, why can't I use it in switch statement?

Here are some image:
enter image description here
enter image description here
enter image description here

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

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

发布评论

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

评论(1

各自安好 2025-01-28 21:54:06

根据 java语言规范(jls):

案例标签具有一个或多个案例常数。每个情况常数必须是常数表达式(§15.29)或枚举常数的名称(§8.9.1),或者发生编译时间误差。

您的情况显然不是枚举常数,所以我想您正在尝试使用 常量表达式 但是您的gamemode class class不限定为constant表达式

常数表达式是一种表达式,表示原始类型的值或不突然完成的字符串,仅使用以下 ...

According to Java Language Specification (JLS):

A case label has one or more case constants. Every case constant must be either a constant expression (§15.29) or the name of an enum constant (§8.9.1), or a compile-time error occurs.

Your case is obviously not an enum constant, so I guess you are trying to use a constant expression but your GameMode class does not qualify as a constant expression.

A constant expression is an expression denoting a value of primitive type or a String that does not complete abruptly and is composed using only the following...

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