从静态方法访问非静态的枚举值

编程入门 行业动态 更新时间:2024-10-24 10:24:41
本文介绍了从静态方法访问非静态的枚举值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 public enum sEnum { zero = 0, one = 1 } public int x; public static void a(sEnum s) { x = 3; if (s == sEnum.one) ... }

为什么能枚举值在这里检查,因为static关键字不使用?这哪里是在语言规范记录?

Why can values of the enum be checked here, as the static keyword isn't used? Where is this documented in the language specification?

推荐答案

枚举刚刚命名的值,因此您可以在静态情况下,就像使用它们任何其他不变。

Enums are just named values so you can use them in a static context just like any other constant.

语言规范状态的第3.4.3节:

Section 3.4.3 of the language specification states:

的成员枚举是在枚举中声明的常量

The members of an enumeration are the constants declared in the enumeration

更多推荐

从静态方法访问非静态的枚举值

本文发布于:2023-07-15 10:22:21,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1110640.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:静态   方法

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!