我似乎无法像从内部类内部那样从枚举内部访问周围类的实例成员。这是否意味着枚举是静态的?是否可以访问周围类的实例的范围,还是必须在需要的地方将实例传递给枚举的方法?
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;
}
}
白衣染霜花
沧海一幻觉
相关分类