在下面的示例中,函数如何访问对象的私有变量?

时间:2016-12-16 14:35:25

标签: java class private access

我正在通过这个例子,在这个link中,我想知道类在代码示例中如何访问私有变量锁。请解释我的优点!!代码在这里,yourLock = bower.lock.tryLock();。

public class Safelock {
static class Friend {
    private final String name;
    private final Lock lock = new ReentrantLock();

    public Friend(String name) {
        this.name = name;
    }

    public String getName() {
        return this.name;
    }

    public boolean impendingBow(Friend bower) {
        Boolean myLock = false;
        Boolean yourLock = false;
        try {
            myLock = lock.tryLock();
            yourLock = bower.lock.tryLock();
        } finally {
            if (! (myLock && yourLock)) {
                if (myLock) {
                    lock.unlock();
                }
                if (yourLock) {
                    bower.lock.unlock();
                }
            }
        }
        return myLock && yourLock;
    }

0 个答案:

没有答案