我有这样的抽象类和接口(interface)
abstract class ParentClass
{
int VALUE;
public abstract void display();
public void display2()
{
System.out.println("this is abstract class method");
}
}
interface parentInterface
{
int VALUE=88;
abstract void display();
void display2();
}
子类扩展并实现了上面的内容,如下所示
class ChildClass extends ParentClass implements parentInterface
{
ChildClass()
{
super.VALUE=0;
//VALUE=0; //<=will give ambiguous property
}
@Override
public void display()
{
System.out.println("Current Class-"+this.getClass());
System.out.println("Integer value-"+super.VALUE);
}
public void display2()
{
//to call the method of abstract class
//call by using super.display2();
System.out.println("this is implemented method");
}
}
所以,我的问题是如何访问 ChildClass 中的接口(interface) VALUE
变量??
最佳答案
您可以使用parentInterface.VALUE
从接口(interface)访问VALUE
,因为接口(interface)中的变量是public static final
by default .
并且抽象类的 VALUE
可以使用 this.VALUE
访问,因为它是一个成员变量。
关于java - 在接口(interface)和抽象类中访问同名值-Java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14118000/