我有两节课
package a;
public class A {
protected void doSomething() {
}
protected static class C {
protected C(int c) {
}
}
}
package b;
public class B extends A {
@Override
protected void doSomething() {
C c = new C(0); //compile error
C c2 = new C(0){}; //legal
}
}
Run Code Online (Sandbox Code Playgroud)
我已阅读第 6.6.2.2 章。访问受保护的 JLS 构造函数(https://docs.oracle.com/javase/specs/jls/se11/html/jls-6.html),但我仍然对解释感到困惑。new C(0);即使 B 是 A 的孩子,调用超级构造函数有什么问题?
谢谢 :-)
我对java.util.PriorityQueue和我自己的Comparator的这个小例子感到非常困惑:
在这段代码中,我在队列中得到了错误的顺序。结果是:5,8,7而不是5,7,8
我有什么问题Comparator<Vertex>吗?谢谢您的帮助。
public class Test {
public static void main(String[] args) {
PriorityQueue<Vertex> priorityQueue = new PriorityQueue<Vertex>(new Comparator() {
@Override
public int compare(Object o1, Object o2) {
Vertex u = (Vertex) o1;
Vertex v = (Vertex) o2;
return Integer.compare(new Integer(u.distance), new Integer(v.distance));
}
});
Vertex vertex1 = new Vertex(1);
Vertex vertex2 = new Vertex(2);
Vertex vertex3 = new Vertex(3);
Vertex vertex4 = new Vertex(4);
vertex1.distance = 8;
vertex2.distance = 5;
vertex3.distance = 7;
priorityQueue.add(vertex1); …Run Code Online (Sandbox Code Playgroud)