• java_闭包和回调实现一边按键盘一边演讲


    package ming;
    
    interface Teachable {
    	void work();
    }
    
    class Programer {
    	private String name;
    
    	public String getName() {
    		return name;
    	}
    
    	public void setName(String name) {
    		this.name = name;
    	}
    
    	public Programer() {
    	}
    
    	public Programer(String name) {
    		this.name = name;
    	}
    
    	public void work() {
    		System.out.println(name + " 正在敲键盘");
    	}
    }
    
    class TeachableProgramer extends Programer {
    
    	public TeachableProgramer() {
    	}
    
    	public TeachableProgramer(String name) {
    		super(name);
    	}
    
    	private void teach() {
    		System.out.println(this.getName() + " 教师正在教学");
    	}
    	//内部类回调外部类实现work方法,内部类只是为了提供一个回调外部类的途径
    	private class Closure implements Teachable {
    
    		@Override
    		public void work() {
    			teach();
    		}
    
    	}
    	//非静态内部类引用允许外部类通过该非态内部类引用回调外部类方法
    	public Teachable getCallbackReference() {
    		return new Closure();
    	}
    
    }
    public class TestClosured {
    
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		TeachableProgramer tp = new TeachableProgramer("zhangsan");
    		tp.getCallbackReference().work();
    		tp.work();
    		
    	}
    
    }
    


  • 相关阅读:
    python基础语法
    java关键字之final
    汉字占几个字节?
    Java学习记录(补充二:面对对象基础复习,习题)
    Java学习记录(补充二)
    Java学习记录(补充一(以习题为主:判断,循环语句))
    Java学习记录
    JS学习记录(jQuery补充一)
    JS学习记录(jQuery)
    JS学习记录(数组补充一)
  • 原文地址:https://www.cnblogs.com/MarchThree/p/3720470.html
Copyright © 2020-2023  润新知