java的接口回调
2016-05-02 14:44
465 查看
1、一个接口对象,一个方法的回调
2、一个接口对象和接口方法
接口对象,通过方法的参数传进去
3、接口,方法
4、一个接口,方法,公共方法
实现接口,实现方法
实例化对象
5、最常用的回调
6、常用回调2
实例化对象,使用传入的参数
public interface InterestingEvent { public void interestingEvent(); }通过集成的方式,并且通过构造函数传入接口对象
public class EventNotifier { private InterestingEvent ie; public EventNotifier(InterestingEvent event) { ie = event; } public void doWork() { // 检查在别处设置的谓词。 // 通过调用接口的这个方法发出事件信号。 ie.interestingEvent(); // ... } public static void main(String[] args) { EventNotifier call = new EventNotifier(new InterestingEvent() { @Override public void interestingEvent() { System.out.println("传入一个接口对象,实例化对象的时候需要实现接口方法,但是这个接口方法是通过其他方法触发的"); } }); call.doWork(); } }这样,调用一个方法就就会触发调用另外一个方法。
2、一个接口对象和接口方法
public interface InterestingEvent { public void interestingEvent(); }
接口对象,通过方法的参数传进去
public class EventNotifier { public void doWork(InterestingEvent in) { in.interestingEvent(); } public static void main(String[] args) { EventNotifier call = new EventNotifier(); call.doWork(new InterestingEvent() { @Override public void interestingEvent() { System.out.println("接口作为参数传入,就是执行自己的方法的时候必须实现之指定的方法"); } }); } }
3、接口,方法
public interface InterestingEvent { public void interestingEvent(); }第二人
public class EventNotifier { private InterestingEvent ie; public EventNotifier(InterestingEvent event) { ie = event; } public void doWork() { ie.interestingEvent(); } }实现接口,传入对象
public class CallMe implements InterestingEvent { private EventNotifier en; public CallMe() { en = new EventNotifier(this); } @Override public void interestingEvent() { System.out.println("这个是第三方实现的地方.这个只是多传了一个人而已"); } public static void main(String[] args) { CallMe callMe = new CallMe(); callMe.call(); } public void call() { en.doWork(); } }
4、一个接口,方法,公共方法
public class Caller { public interface MyCallInterface{ public void fuc(); } private MyCallInterface mc; public Caller() { } //暴露公共的方法 public void setI(MyCallInterface mc) {//传入接口对象 this.mc = mc; } //虽然是调用者.但是如果不传入具体的方法体还是没有实际内容的 public void call() { mc.fuc(); } /* * caller是调用者,callee是被调用者,callbacks表示调用过程。 */ }
实现接口,实现方法
//这个类实现了同一接口.将被传入. 真的方法的实现者 public class Callee implements MyCallInterface{ @Override public void fuc() { System.out.println("hahafdf"); } /* * caller是调用者,callee是被调用者,callbacks表示调用过程。 */ public static void main(String[] args) { Callee callee = new Callee(); Caller caller = new Caller(); caller.setI(callee);//把真的实现方式植入 caller.call(); } /* * 实现接口.就可以被植入,然后就可以做为方法的具体实现着 */ }
实例化对象
public class Callbacks { public static void main(String args[]){ Callee c1=new Callee();//一个实现了指定接口的类 Caller caller=new Caller(); caller.setI(c1);//因为c1实现了同一接口.所以可以被传入 caller.call(); } /* * 先产生了Callee对象,利用这个callee对象产生的Caller对象则携带了一些信息(即与Callee对象的关联), * 所以Caller对象可以利用自己的call方法调用Callee的方法。——这就是整个回调过程。 */ }
5、最常用的回调
接口.方法.暴露方法.使用接口方法定义里面的参数
//这个类实现了同一接口.将被传入. 真的方法的实现者 public class Callee implements MyCallInterface{ /* * caller是调用者,callee是被调用者,callbacks表示调用过程。 */ public static void main(String[] args) { Callee callee = new Callee(); Caller caller = new Caller(); caller.setI(callee);//把真的实现方式植入 caller.call(5); //调用者可以讲参数传递给 } /* * 实现接口.就可以被植入,然后就可以做为方法的具体实现着 */ @Override public void fuc(int i) { System.out.println(i+"");//需要使用传入进来的参数 } //实现了接口,然后又被传入了.所以就可以直接调用里面的方法 //应用:一个地方的值传入到另外一个地方,随着改变而改变 }
实现接口.被植入.传入具体的参数
public class Caller { public interface MyCallInterface{ public void fuc(int i); } private MyCallInterface mc; public Caller() { } //暴露公共的方法 public void setI(MyCallInterface mc) {//传入接口对象 this.mc = mc; } //虽然是调用者.但是如果不传入具体的方法体还是没有实际内容的 public void call(int i) { mc.fuc(i); System.out.println("使用传递进来的i值:"+i); } /* * caller是调用者,callee是被调用者,callbacks表示调用过程。 */ }
6、常用回调2
接口,方法(有参数)[可以单独定义]作为接口对象传入参数值
public class EventNotifier { //接口对象最参数,把这个类的 private static int NUM = 3; public void doWork(InterestingEvent in) { //NUM是这个类里面的对象 in.interestingEvent(NUM); } //接口对象,方法 public interface InterestingEvent { public void interestingEvent(int i); } }
实例化对象,使用传入的参数
public class AnotherUser { public static void main(String[] args) { EventNotifier en = new EventNotifier(); en.doWork(new InterestingEvent() { @Override public void interestingEvent(int i) { System.out.println("这个i是传递过来的i:"+i); } }); } }
相关文章推荐
- SpringData 学习(5)—— 使用 @Modifying 注解完成修改操作
- Java本地文件操作(四)文件属性的设置
- 多态的表现特点
- 用堆排序实现查找最小的K个元素 java
- java 对缺乏潜在类型机制的补偿 :反射
- Java本地文件操作(三)文件属性的读取
- Part 2 How to Monitor Java Garbage Collection
- RxJava结合ProgressDialog实现请求数据
- java构造类
- Part 1 Understanding Java Garbage Collection
- 第4章 控制执行流程
- eclipse创建maven模块工程
- Java本地文件操作(二)文件夹的创建、删除、重命名
- java权限修饰符
- 码农小汪-Spring MVC url路径,请求方法限定
- 9.2 JavaScritp:为数据带来生命-数组对象
- java中的线程问题
- SpringMVC访问静态资源的三种方式
- org.springframework.beans.factory.NoUniqueBeanDefinitionException
- Java 线程局部存储