public class TestInterface implements a1,a2{
public static void main(String args[]) {
TestInterface t =new TestInterface();
System.out.println(j);
//System.out.println(i);
}
}interface a1 {
int i = 1;
}interface a2 {
int j = 2;
//int i = 3;
}
接口中的常量都是 public static finally
java这样做是为了解决C++多继承中同名变量产生的问题,可是如果我把注释去掉,编译通过,语法是没有问题的,可是,这样还是没有解决C++多继承的问题啊,如果要输出i,那还是会出问题,我想问问java中申明的接口类型中常量都是public static finally有什么用?
public static void main(String args[]) {
TestInterface t =new TestInterface();
System.out.println(j);
//System.out.println(i);
}
}interface a1 {
int i = 1;
}interface a2 {
int j = 2;
//int i = 3;
}
接口中的常量都是 public static finally
java这样做是为了解决C++多继承中同名变量产生的问题,可是如果我把注释去掉,编译通过,语法是没有问题的,可是,这样还是没有解决C++多继承的问题啊,如果要输出i,那还是会出问题,我想问问java中申明的接口类型中常量都是public static finally有什么用?
因此设计它的时候就必须考虑到这一点,并且让应用它的类之间不会出现冲突。如果interface中的变量不是static final,它就可能被任何一个应用它的类改变,这时,其它应用它的类就可能取到错误的值。谁来为它们负责?可以把它假想为图书馆的一本书,任何人都可以借出它,阅读它。但任何人都不能在上面涂改,这是为了所有读者的利益。