public class Deadlock {
static class Friend {
private final String name;
public Friend(String name) {
this.name = name;
}
public String getName() {
return this.name;
}
public synchronized void bow(Friend bower) {
System.out.format("%s: %s"
+ " has bowed to me!%n",
this.name, bower.getName());
bower.bowBack(this);
}
public synchronized void bowBack(Friend bower) {
System.out.format("%s: %s"
+ " has bowed back to me!%n",
this.name, bower.getName());
}
}
public static void main(String[] args) {
final Friend alphonse =
new Friend("Alphonse");
final Friend gaston = …
Run Code Online (Sandbox Code Playgroud) 我已经读过子类不能继承私有字段或方法.但是,在这个例子中
class SuperClass {
private int n=3;
int getN() {
return n;
}
}
class SubClass extends SuperClass {
public static void main(String[] args) {
SubClass e = new SubClass();
System.out.println("n= " + e.getN());
}
}
Run Code Online (Sandbox Code Playgroud)
当我运行时,main
我得到输出为n=3
.这似乎SubClass
是继承了私有属性n
的SuperClass
.
所以,请解释这里发生了什么.谢谢.