2012-02-16 43 views
1

可以說我有一個接口字段 - 字符串類型=「接口」。
它的實現類有一個字段 - String type =「class」。
有什麼方法可以通過該類訪問界面的字段嗎?訪問接口的重寫變量

回答

2

是的..因爲基本上接口變量是公共靜態最終還是在其他的字,一個常數..

您可以使用一個靜態的方式訪問您的

IYourInterfaceName.type 
+0

+1爲解釋爲什麼這個工程。 – 2012-02-16 05:16:07

2
public interface Firstone { 
String type="interface"; 
} 
public class Abc implements Firstone { 

/** 
* @param args 
*/ 
String type="class"; 
void check(){ 

    System.out.println("my class\t"+type); 
    System.out.println("my interface\t"+Firstone.type); 
} 

public static void main(String[] args) { 

    Abc a=new Abc(); 
    a.check(); 
} 

}