interface t{
int counter = 40;

}class hello implements t{
private static int counter; public static void main(String[] args) throws FileNotFoundException {
System.out.println(counter);
}
}请问为什么输出是0啊??

解决方案 »

  1.   

    counter没有继承过来,int类型自动初始化为0;
      

  2.   

    因为private static int counter;
    所以父类中的变量被覆盖了。
      

  3.   

    通过接口名可以使用接口t的属性counter. 接口里的属性都是static,final的。楼主加一句看看:System.out.println(t.counter);
      

  4.   

    接口就是用来被重写,要是在接口中想定义属性,必须得是final类型的,也就是不可改变的属性,但是你定义的是可以改变的,应该这样定义:final int counter = 40;
      

  5.   

    final int counter = 40;