public class InterfaceTest {
interface InterfaceA {
int len = 1 ;
void output();
}
interface InterfaceB {
int len = 2 ;
void output();
}
interface InterfaceSub extends InterfaceA, InterfaceB { }
public class Xyz implements InterfaceSub {
public void output() {
System.out.println( "output in class Xyz." );
}
public void outputLen(int type) {
switch (type) {
case InterfaceA.len:
System.out.println( "len of InterfaceA=." +type);
break ;
case InterfaceB.len:
System.out.println( "len of InterfaceB=." +type);
break ;
}
}
}
public static void main(String[] args) {
Xyz xyz = new Xyz();
xyz.output();
xyz.outputLen(1);
}
}
你好,我想学习Java的接口和多继承概念。我找到了上面的代码并尝试编译它,但出现以下错误。我不知道如何使代码工作,谁可以帮忙?谢谢!
test$ javac InterfaceTest.java
InterfaceTest.java:33: error: non-static variable this cannot be referenced from a static context
Xyz xyz = new Xyz();
^
1 error