从静态方法访问非静态的枚举值 [英] Accessing non-static enum values from static methods

查看:88
本文介绍了从静态方法访问非静态的枚举值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

这篇关于从静态方法访问非静态的枚举值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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