2013-08-31 49 views
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 
    } 
} 

編譯器抱怨如下編譯器錯誤,而超載 - Java的

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

給定的輸入是0L,我試圖重載在子類的DISP()方法。

回答

4

方法disp()被聲明爲private

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

因此,它只是child類中可見,不是從impl類,你試圖調用它。將其可見性更改爲public或重新考慮您的設計。

如果你的問題是爲什麼它看到disp(long)而不是disp(int),這是因爲你提供了一個long原始值的方法調用。

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

訪問修飾符的官方教程是here

+0

「不是從您正在嘗試呼叫的impl類」...感謝指出這:) – UnderDog