小编har*_*ish的帖子

两个线程可以同时访问同步方法吗?

    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)

java multithreading

12
推荐指数
2
解决办法
2万
查看次数

私有字段是否由子类继承?

我已经读过子类不能继承私有字段或方法.但是,在这个例子中

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是继承了私有属性nSuperClass.

所以,请解释这里发生了什么.谢谢.

java inheritance private-members

9
推荐指数
2
解决办法
1万
查看次数