0
class parent
{
    public void disp(int i)
    {
        System.out.println("int");
    }
}

class child extends parent
{
    private void disp(long l)
    {
        System.out.println("long");
    }
}

class impl
{
    public static void main(String... args)
    {
        child c = new child();
        c.disp(0l); //Line 1
    }
}

编译器抱怨如下

inh6.java:27: error: disp(long) has private access in child
c.disp(0l);

给定的输入是 0L,我正在尝试重载子类中的 disp() 方法。

4

1 回答 1

4

The method disp() is declared as private

private void disp(long l){System.out.println("long");}

As such, it is only visible within the child class, not from the impl class where you are trying to call it. Either change its visibility to public or rethink your design.

If your question is why is it seeing the disp(long) instead of disp(int), this is because you provided a long primitive value to the method call.

 c.disp(0l); // the 'l' at the end means 'long'

The official tutorial for access modifiers is here.

于 2013-08-31T16:28:30.423 回答