为什么枚举实现不会访问枚举类中的私有字段 [英] Why can enum implementations not access private fields in the enum class

查看:117
本文介绍了为什么枚举实现不会访问枚举类中的私有字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚通过说如何解决编译问题回答了这个问题:



如何通过覆盖方法来使用java枚举中的字段



以下是以枚举形式写入的示例:

  public enum MyEnum {


FIRST {
@Override
public String doIt (){
return1:+ someField; // error
}
},
SECOND {
@Override
public String doIt(){
return2:+ super.someField; //没有错误
}
};

private String someField;


public abstract String doIt();

}

这与抽象类完全相同

 抽象类MyClass {
类FIRST扩展MyClass {
@Override
public String doIt()
return1:+ someField; //没有错误
}
};
class SECOND extends MyClass {
@Override
public String doIt(){
return2:+ super.someField; //没有错误
}
};

private String someField;

public abstract String doIt();
}

FIRST 枚举实现中,它不能访问 someField 。但是在抽象类的情况下它可以。



另外添加 super 可以解决问题,同样删除 private 修饰符的字段。



有谁知道为什么这种轻微的怪癖在行为中发生?

解决方案

你的抽象类不等同于你的枚举,因为枚举是隐式的public static final。因此,如果您使用以下方式,您将遵循相同的行为:

 抽象类MyClass {

static class FIRST extends MyClass {

@Override
public String doIt(){
return1:+ someField; // error
}

};

static class SECOND extends MyClass {

@Override
public String doIt(){
return2:+ super.someField; //没有错误
}

};

private String someField;

public abstract String doIt();

}

http://docs.oracle.com/javase/tutorial/java/javaOO/nested.html , 静态嵌套类一章:


静态嵌套类不能直接引用实例变量或在其封闭类中定义的
方法:它只能通过
一个对象引用来使用它们。


因此,需要 super 。如果该字段为 protected 而不是 private c>。


I just answered this question by saying how to solve the compilation problem:

How to use fields in java enum by overriding the method?

But what I don't understand is why the error is happening in the first place.

Here is the example written as an enum:

public enum MyEnum {


    FIRST {
        @Override
        public String doIt() {
            return "1: " + someField; //error
        }
    },
    SECOND {
        @Override
        public String doIt() {
            return "2: " + super.someField; //no error
        }
    };

    private String someField;


    public abstract String doIt();

} 

Here is the exact same thing as abstract classes

abstract class MyClass {
    class FIRST extends MyClass {
        @Override
        public String doIt() {
            return "1: " + someField; //no error
        }
    };
    class SECOND extends MyClass {
        @Override
        public String doIt() {
            return "2: " + super.someField; //no error
        }
    };

    private String someField;

    public abstract String doIt();
}

In the case of FIRST within the enum implementation it cannot access someField. However in the abstract class case it can.

Additionally adding super fixes the problem, as does removing the private modifier on the field.

Does anyone know why this slight quirk in the behaviour is happening?

解决方案

Your abstract class is not equivalent to your enum, since enums are implicitly public static final. Thus, you'll observe the same behavior if you use:

abstract class MyClass {

    static class FIRST extends MyClass {

        @Override
        public String doIt() {
            return "1: " + someField; // error
        }

    };

    static class SECOND extends MyClass {

        @Override
        public String doIt() {
            return "2: " + super.someField; // no error
        }

    };

    private String someField;

    public abstract String doIt();

}

As explained in http://docs.oracle.com/javase/tutorial/java/javaOO/nested.html, chapter "Static Nested Classes":

A static nested class cannot refer directly to instance variables or methods defined in its enclosing class: it can use them only through an object reference.

Thus the need of super. You could also use this if the field were protected rather than private.

这篇关于为什么枚举实现不会访问枚举类中的私有字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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