Thinking in Java 10.8.1 闭包与回调

功能:
过程:

类CallBacks
仅含main方法:
调用类MyIncrement的f方法
调用类Caller的go方法2遍(参数为类Callee1)
调用类Caller的go方法2遍(参数为类Callee2)

  • 接口Incrementable
    方法increment()

  • 类Caller
    以Incrementable为参数的构造函数
    go方法,调用Incrementable的increment()方法

  • 类MyIncrement
    打印"Other operation"的公共方法increment()
    静态方法f,调用increment()

  • 类Callee2继承MyIncrement类 覆盖方法increment() 调用上级方法,打印从1开始,步长1
    内部类Closure实现Incrementable 上级的increment()方法
    getCallbackReference,返回Closure

  • 类Callee1实现接口Incrementable
    从1开始打印,步长1

个人实现:

package thinking;

interface Incrementable{
	void increment();
}

class Caller{
	Incrementable incrementable;
	Caller(Incrementable incrementable){
		this.incrementable = incrementable;
	}
	void go(){
		incrementable.increment();
	}
}

class MyIncrement{
	public void increment() {
		System.out.println("Other operation");
	}
	//未写出此方法
	static void f(MyIncrement myIncrement){
		myIncrement.increment();
	}
}

class Callee2 extends MyIncrement{
	//i 应定义在此,否则i++等无意义
	public void increment(){
		super.increment();
		int i = 0;
		i++;
		System.out.println(i);
	}
	class Closure implements Incrementable{

		@Override
		public void increment() {
			//未写出此方法
			Callee2.this.increment();
		}
		
	}
	Incrementable getCallbackReference(){
		return new Closure();
	}
}

class Callee1 implements Incrementable{

	@Override
	public void increment() {
		int i = 0;
		i++;
		System.out.println(i);
		
	}
	
}

public class CallBacks {

	public static void main(String[] args) {
		Callee2 callee2 = new Callee2();
		Callee1 callee1 = new Callee1();
		Caller caller1 = new Caller(callee1);
		Caller caller2 = new Caller(callee2.getCallbackReference());
		MyIncrement.f(callee2);
		caller1.go();
		caller1.go();
		caller2.go();
		caller2.go();

	}
}

原书实现:

package thinkingjava;

interface Incrementable{
	void increment();
}

class Caller{
	private Incrementable callbackReference;
	Caller(Incrementable cbn){
		callbackReference = cbn;
	}
	void go(){
		callbackReference.increment();
	}
}

class MyIncrement{
	public void increment() {
		System.out.println("Other operation");
	}
	//未写出此方法
	static void f(MyIncrement myIncrement){
		myIncrement.increment();
	}
}

class Callee2 extends MyIncrement{
	private int i = 0;
	public void increment(){
		super.increment();
		i++;
		System.out.println(i);
	}
	private class Closure implements Incrementable{

		@Override
		public void increment() {
			Callee2.this.increment();
		}
		
	}
	Incrementable getCallbackReference(){
		return new Closure();
	}
}

class Callee1 implements Incrementable{

	private int i = 0;
	@Override
	public void increment() {
		i++;
		System.out.println(i);
		
	}
	
}

public class CallBacks {

	public static void main(String[] args) {
		Callee2 callee2 = new Callee2();
		Callee1 callee1 = new Callee1();
		MyIncrement.f(callee2);
		Caller caller1 = new Caller(callee1);
		Caller caller2 = new Caller(callee2.getCallbackReference());
		
		caller1.go();
		caller1.go();
		caller2.go();
		caller2.go();

	}
}

比较:区别1见个人实现注释,2为个人很少使用private

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值