accessing interface's overridden variable

廉价感情. 提交于 2019-12-13 02:25:27

问题


Lets say I have an interface with a field - String type = "interface".
It's implementing class has a field - String type = "class".
Is there any way to access the interface's field through that class anymore?


回答1:


Yep.. because basically interface variable are public static final or in other word, a constant..

you can access it in a static way using your

IYourInterfaceName.type



回答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();
}

}



来源:https://stackoverflow.com/questions/9305364/accessing-interfaces-overridden-variable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!