java的接口回调

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() {
			@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);
			}
		});
	}
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值