package com;
public interface ITest {
public void test();
}
===========================================
package com;
public class Test1 implements ITest {
@Override
public void test() {
// TODO Auto-generated method stub
System.out.println("调用成功");
}
}
======================================
package com;
public class Test2 {
private static ITest iTest;
public static void main(String[] args) {
// TODO Auto-generated method stub
iTest.test();
}
public ITest getiTest() {
return iTest;
}
public void setiTest(ITest iTest) {
this.iTest = iTest;
}
}
为什么返回值为null???第一次用sf,求大神指点
You just declared a variable named iTest and did not instantiate it. In JAVA language, you usually use the NEW keyword to instantiate an object.
Change your code above to the following:
itest is a static variable, not initialized, is null, and its method cannot be called. Must be initialized first.
Change it here to private static ITest iTest= new Test1()
Because you don’t have new