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如何去扩展一个接口呢? 我这个是在百度查的 答案是通过继承,这一点我刚开始不是很理解,我一直以为只有类是可以集成的,但是没有想到接口也可以继承,下面有个例子可以做个参考:
注意在这时候,如上面的例子中的Intface2接口,如果实现这个接口的话,必须同时将Intface1接口中的方法给实现了,否则编译器会报错。
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 | } |
相关文章推荐
- java接口实例化问题
- Java中接口及抽象类的实例化问题
- java接口不能实例化原因浅谈
- java接口实例化问题
- Java中接口与实例化
- java的接口为什么不能实例化
- java web中常用对象对应的实例化接口
- Java 接口“实例化”分析
- 使用Java匿名类直接实例化接口
- java接口中能申明私有成员么?接口能不能实例化?
- java接口中能申明私有成员么?接口能不能实例化?
- 关于Android(JAVA)实例化接口假象的讨论
- java接口的”实例化“
- java 为什么接口不能实例化
- java的接口不能实例化,但是有些函数可以这样写
- 好记性不如烂笔头-java接口只能声明对象,但不能实例化对象
- java接口不能实例化原因浅谈
- [Java基础]接口能否被实例化以及匿名内部类
- Java中接口与实例化【引用】
- java-接口现实实例化