我似乎无法从枚举中访问周围类的实例成员,因为我可以从内部类中访问.这是否意味着枚举是静态的?是否可以访问周围类的实例的范围,或者我是否必须将实例传递到我需要它的枚举方法中?
public class Universe {
public final int theAnswer;
public enum Planet {
// ...
EARTH(...);
// ...
// ... constructor etc.
public int deepThought() {
// -> "No enclosing instance of type 'Universe' is accessible in this scope"
return Universe.this.theAnswer;
}
}
public Universe(int locallyUniversalAnswer) {
this.theAnswer = locallyUniversalAnswer;
}
}
Run Code Online (Sandbox Code Playgroud)