为什么内部类可以访问私有方法? [英] Why do inner classes make private methods accessible?

查看:116
本文介绍了为什么内部类可以访问私有方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不明白为什么这会编译。 f()和g()在内部类中是可见的,尽管是私有的。他们是特殊的,因为他们是内部阶级吗?

I don't understand why this compiles. f() and g() are visible from the inner classes, despite being private. Are they treated special specially because they are inner classes?

如果A和B不是静态类,它仍然是相同的。

If A and B are not static classes, it's still the same.

class NotPrivate {
    private static class A {
        private void f() {
            new B().g();
        }
    }

    private static class B {
        private void g() {
            new A().f();
        }
    }
}


推荐答案

(编辑:扩展答案以回答一些评论)

( expanded on the answer to answer some of the comments)

编译器获取内部类并将它们转换为顶级类。由于私有方法仅对内部类可用,因此编译器必须添加具有包级访问权限的新合成方法,以便顶级类可以访问它。

The compiler takes the inner classes and turns them into top-level classes. Since private methods are only available to the inner class the compiler has to add new "synthetic" methods that have package level access so that the top-level classes have access to it.

像这样的东西($ 1由编译器添加):

Something like this (the $ ones are added by the compiler):

class A 
{
    private void f() 
    {
        final B b;

        b = new B();

        // call changed by the compiler
        b.$g();
    }

    // method generated by the compiler - visible by classes in the same package
    void $f()
    {
        f();
    }
}

class B
{
    private void g() 
    {
        final A a;

        a = new A();

        // call changed by the compiler
        a.$f();
    }

    // method generated by the compiler - visible by classes in the same package
    void $g()
    {
        g();
    }
}

非静态类是相同的,但它们有添加对外部类的引用,以便可以在其上调用方法。

Non-static classes are the same, but they have the addition of a reference to the outer class so that the methods can be called on it.

Java这样做的原因是他们不想要求VM更改以支持内部类,因此所有更改都必须在编译器级别。

The reason Java does it this way is that they did not want to require VM changes to support inner classes, so all of the changes had to be at the compiler level.

编译器获取内部类并将其转换为顶级类(因此,在VM级别,不存在内部类)。然后编译器还必须生成新的转发方法。它们是在包级别(非公共)创建的,以确保只有同一包中的类才能访问它们。编译器还将对私有方法的方法调用更新为生成的转发方法。

The compiler takes the inner class and turns it into a top level class (thus, at the VM level there is no such thing as an inner class). The compiler then also has to generate the new "forwarding" methods. They are made at the package level (not public) to ensure that only classes in the same package can access them. The compiler also updated the method calls to the private methods to the generated "forwarding" methods.

您可以避免编译器生成方法我将方法声明为包 (没有公共,私人和受保护的)。这样做的缺点是包中的任何类都可以调用方法。

You can avoid having the compiler generate the method my declaring the methods as "package" (the absence of public, private, and protected). The downside to that is that any class in the package can call the methods.

编辑:

是的,你可以调用生成的(合成的)方法,但不要这样做!:

Yes, you can call the generated (synthetic) method, but DON'T DO THIS!:

import java.lang.reflect.Constructor;
import java.lang.reflect.Method;

public class Main
{
    public static void main(final String[] argv)
        throws Exception
    {
        final Class<?> clazz;

        clazz = Class.forName("NotPrivate$A");        

        for(final Method method : clazz.getDeclaredMethods())
        {
            if(method.isSynthetic())
            {
                final Constructor constructor;
                final Object instance;

                constructor = clazz.getDeclaredConstructor(new Class[0]);
                constructor.setAccessible(true);
                instance = constructor.newInstance();
                method.setAccessible(true);
                method.invoke(null, instance);
            }
        }
    }
}

这篇关于为什么内部类可以访问私有方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆