您的位置:首页 > 编程语言 > Java开发

java 接口的 实例化

2013-08-19 15:48 253 查看
今天看代码的时候 看到一个这样的用法我注意了一下(我还是比较对C++根深蒂固点 呵呵)

public interface ServiceConnection {

    public void onServiceConnected(ComponentName name, IBinder service);

    public void onServiceDisconnected(ComponentName name);

}

 private ServiceConnection mVcardConnection = new ServiceConnection() {

        @Override

        public void onServiceConnected(ComponentName name, IBinder binder) {

            mVcardService = ((VCardService.MyBinder) binder).getService();

            isVcardBind = true;

            Log.i(TAG, String.format("Connected to VcardService."));

        }

        @Override

        public void onServiceDisconnected(ComponentName name) {

            isVcardBind = false;

            Log.i(TAG, "Disconnected from VCardService");

        }

    };

这算不算实例化接口 看上去很像  这样new 一个接口的时候需要把这个接口的所有的方法都实现  ,接口中是没有构造函数这种说法的

java如何去扩展一个接口呢? 我这个是在百度查的 答案是通过继承,这一点我刚开始不是很理解,我一直以为只有类是可以集成的,但是没有想到接口也可以继承,下面有个例子可以做个参考:

 

01
interface
 
Intface1{
02
    
void
 
test01();
03
}
04
interface
 
Intface2 
extends
 
Intface1{
05
    
void
 
test02();
06
}
07
 
08
public
 
class
 
TestNewInterface 
implements
 
Intface2{
09
    
public
 
void
 
test01(){}
10
    
public
 
void
 
test02(){}
11
}
注意在这时候,如上面的例子中的Intface2接口,如果实现这个接口的话,必须同时将Intface1接口中的方法给实现了,否则编译器会报错。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: