java中interface中定义方法,标准的调用方式
大家讲道理
大家讲道理 2017-04-18 09:20:52
0
4
412
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,求大神指点
大家讲道理
大家讲道理

光阴似箭催人老,日月如移越少年。

reply all(4)
伊谢尔伦

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:

package com;

public class Test2 {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        ITest tmp = new Test1();
        tmp.test();
    }
}
洪涛

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

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template