阅读背景:

使用多态可以破坏Java中受保护的访问修饰符的规则吗?

来源:互联网 

Here's my code

这是我的代码

Package a

package a;

import static net.mindview.util.Print.print;

import b.B;

public class A
{
    protected void f()
    {
        print("This is A's protected f()");
    }

    public static void main(String[] args)
    {
        // new B().f() does not work!
        // compiler will complain B.f() is not visible
        A a = new B();
        a.f();    //  but using polymorphism here! I can invoke B.f()!
    }
}
package a



你的当前访问异常,请进行认证后继续阅读剩余内容。

分享到: