2013-04-14 88 views

回答

5

,你可以做這樣的可能:

class.getMethod("toString").getDeclaringClass(); 

這裏有一個例子:

class Test { 
     public String toString(){ 
       return "From test"; 
     } 
    } 

    public static void main(String[] args) throws NoSuchMethodException, SecurityException { 
    String resut = Test.class.getMethod("toString").getDeclaringClass().getName(); 
    System.out.println(resut); 
} 

現在,隨着覆蓋,然後將其註釋掉的toString冷杉運行這個,你會看到輸出和「測試「,然後」對象「