java的接口回调

来源:互联网 发布:python中的迭代器 编辑:程序博客网 时间:2024/04/28 18:07

1、一个接口对象,一个方法的回调

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() {@Overridepublic 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() {@Overridepublic 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);}@Overridepublic 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{@Overridepublic 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);//调用者可以讲参数传递给}/* * 实现接口.就可以被植入,然后就可以做为方法的具体实现着 */@Overridepublic 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() {@Overridepublic void interestingEvent(int i) {System.out.println("这个i是传递过来的i:"+i);}});}}



0 0
原创粉丝点击