当前位置: 代码迷 >> J2SE >> 关于static方法中只能引用static方法的有关问题
  详细解决方案

关于static方法中只能引用static方法的有关问题

热度:2452   发布时间:2013-02-25 00:00:00.0
关于static方法中只能引用static方法的问题
Java code
public class test5 {    public static void main(String[] args) {    //    c2.c2_func1();    c2.c2_func2(new c2());    }}interface I1 {    void I1_func1();}class c2 implements I1{    public void I1_func1() {System.out.println("I1_func1()");}    //  static void c2_func1() {I1_func1();} /* complier error */    static void c2_func2(I1 o) {o.I1_func1();} /* pass! */}


看代码,为什么同样是在static方法里调用实例方法,一个通过编译正常运行,而另一个通不过呢?

在这里 interface 起到什么作用,难道实现了interface中声明的函数的方法自动成了static的?

------解决方案--------------------------------------------------------
个人浅见:

I1_func1() 不是static的,是属于实例方法,那么一定是要通过对象调用的。
Java code
 static void c2_func1() {I1_func1();} /* complier error */
------解决方案--------------------------------------------------------
注意:static方法内是可以调用非static方法的

static方法调用非static方法的前提条件是需要对象引用的
  相关解决方案