默认情况下,枚举中变量的访问级别是多少 [英] What is the access level of variables in enums by default

查看:105
本文介绍了默认情况下,枚举中变量的访问级别是多少的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近我遇到了以下一段代码:

Recently I've come across the following piece of code:

enum Animals {
    DOG("woof"), CAT("meow"), FISH("burble");
    String sound;

    Animals(String s) {
        sound = s;
    }
}

class TestEnum {
    static Animals a;
    public static void main(String[] args) {
        System.out.println(a.DOG.sound + " " + a.FISH.sound);//Expected compilation failure
    }
}

我希望代码因为这个<$ c而无法编译$ c> a.DOG.sound 部分。但令我惊讶的是它没有。我已经搜索过各种各样的内容,包括官方文档以找出访问级别但什么也没找到。是公开还是默认

I would expect the code to fail to compile because of this a.DOG.sound part. But to my surprise it doesn't. I've searched all around including the official documentation to find out the access level but found nothing. Is it public or default?

推荐答案

隐式访问级别枚举中手动声明的字段是 package-private ,与普通类中的字段完全相同。因此,当且仅当动物 TestEnum 声音字段$ c>在同一个包中。

The implicit access level of a manually declared field in an enum is package-private, exactly the same as it in normal classes. Thus your sound field will be accessible if and only if Animals and TestEnum are in the same package.

我试图在JLS中找到一个可靠的引用但枚举不幸的是,规则分散在整个地方,被指定为普通类规则的例外,因此必须从各个部分组合规则。 JLS§6.6.1确定辅助功能说:

I tried to find a solid quote for this in the JLS but the enum rules are unfortunately scattered all over the place, specified as exceptions to the rules for normal classes, and the rules thus have to be assembled from pieces. JLS §6.6.1 Determining Accessibility says:


引用类型的成员(类,接口,字段或方法)或构造函数类型,只有在类型可访问且声明成员或构造函数允许访问时才可访问:

A member (class, interface, field, or method) of a reference type, or a constructor of a class type, is accessible only if the type is accessible and the member or constructor is declared to permit access:


  • 如果成员或构造函数声明为 public ,然后允许访问。

缺少访问修饰符的接口的所有成员都隐含 public

All members of interfaces lacking access modifiers are implicitly public.

否则,如果声明成员或构造函数 protected ,只有在满足以下条件之一时才允许访问:

Otherwise, if the member or constructor is declared protected, then access is permitted only when one of the following is true:


  • 访问成员或构造函数从包含声明 protected 成员或构造函数的类的包中。

  • Access to the member or constructor occurs from within the package containing the class in which the protected member or constructor is declared.

访问是正确的,如§6.6.2所述。

Access is correct as described in §6.6.2.

否则,如果使用包访问声明成员或构造函数,然后只有在声明类型的包内发生访问时才允许访问。

Otherwise, if the member or constructor is declared with package access, then access is permitted only when the access occurs from within the package in which the type is declared.

A类没有访问修饰符声明的成员或构造函数隐式具有包访问权。

否则,成员或构造函数被声明为 private ,并且当且仅当它发生在包含成员或构造函数声明的顶级类(第7.6节)的主体内时才允许访问。

Otherwise, the member or constructor is declared private, and access is permitted if and only if it occurs within the body of the top level class (§7.6) that encloses the declaration of the member or constructor.

这意味着类类型( class enum )获取成员隐式具有包访问权限的规则,而接口类型( interface @interface )得到成员隐含公开的规则。

This means that class types (class and enum) get the rule that members implicitly have package access, while interface types (interface and @interface) get the rule that members are implicitly public.

它不是立即的从上面可以明显看出,类成员在其类的定义中包含了枚举,但确实如此。由于它们的广泛重叠,JLS组在许多地方枚举了类(并且注释类型同样与接口组合)。 JLS§8.9枚举类型说枚举声明指定一个新的枚举类型,一种特殊的类类型;和JLS§8.2班级成员明确指出类成员一词是指类类型的成员。

It is not immediately obvious from the above that "class member" includes enums in its definition of "class", but it does. Because of their broad overlap, the JLS groups enums with classes in many places (and annotation types get likewise grouped with interfaces). JLS §8.9 Enum Types says "An enum declaration specifies a new enum type, a special kind of class type"; and JLS §8.2 Class Members makes clear that the term "class members" means members of a "class type".

然而,枚举确实得到两条关于成员可访问性的特殊规则包含在上面的引用部分中:

However, enums do get two special rules with regard to member accessibility that are not included in the quoted section above:


  1. 枚举常量本身(在您的示例中它们是 DOG CAT FISH )可能没有任何明确的访问修饰符(JLS§8.9。 1 ),并且始终是枚举类型的公共静态最终字段(JLS§8.9.3)。

  1. The enum constants themselves (in your example they are DOG, CAT, and FISH) may not have any explicit access modifiers (JLS §8.9.1), and are always public static final fields of the enum type (JLS §8.9.3).

枚举构造函数必须是私有的(以防止人们创建额外的常量)并且是隐式私有的(JLS§8.9.2

Enum constructors must be private (to prevent people creating extra constants) and are private implicitly (JLS §8.9.2).

除了这两个例外,普通类的访问规则适用于枚举。如果您的 Animals 枚举是 public ,它及其所有常量都可以在包外访问,但是声音字段是包私有的,除非您明确声明 public ,否则无法在包外访问。

Apart from those two exceptions, the access rules of normal classes apply to enums. If your Animals enum is made public, it and all its constants are accessible outside the package, but the sound field is package-private, and is not accessible outside the package unless you declare it public explicitly.

这篇关于默认情况下,枚举中变量的访问级别是多少的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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