我的印象是私有内部/嵌套类仅用于外部类/由外部类使用/访问。但是通过实验我意识到HashSet(不是Happy的外部类)可以访问该类。如果是这样,那么真正的用途是private
什么?它是否仅将类的创建限制在外部类中?
public class FreeMain {
private static class Happy {
int x;
private Happy(int x) {
this.x = x;
}
@Override
public boolean equals(Object o) {
return true;
}
@Override
public int hashCode() {
return 10;
}
}
public static void main(String[] args) {
Set<Happy> happiness = new HashSet<Happy>();
Happy h1 = new Happy(10);
Happy h2 = new Happy(20);
happiness.add(h1);
happiness.add(h2);
}
}