⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 test.java

📁 模拟小孩喂食的java程序
💻 JAVA
字号:
package com.anyin.blog;
/*
 * version 0.5
 * 添加读配置文件的功能
 */

/*
 * version 0.6
 * 使用缓存
 */

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

class WakenUpEvent{
	private long time;
	private String location;
	private Child source;
	public WakenUpEvent(long time, String location, Child source) {
		this.time = time;
		this.location = location;
		this.source = source;
	}
	public String getLocation() {
		return location;
	}
	public void setLocation(String location) {
		this.location = location;
	}
	public Child getSource() {
		return source;
	}
	public void setSource(Child source) {
		this.source = source;
	}
	public long getTime() {
		return time;
	}
	public void setTime(long time) {
		this.time = time;
	}
}
class Child implements Runnable{

	List<WakenUpListener> wakenUpListeners = new
		ArrayList<WakenUpListener>();
	
	public void run(){
		try {
			Thread.sleep(1000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		wakeUp();
	}

	private void wakeUp() {
		for(int i=0;i<wakenUpListeners.size();i++){
			WakenUpListener wl = wakenUpListeners.get(i);
			wl.actionToWakenUp(new WakenUpEvent(System.currentTimeMillis(),
					"bed",this));
		}
	}
	
	public void addWakenUpListener(WakenUpListener wl){
		wakenUpListeners.add(wl);
	}
}

class Dad implements WakenUpListener {
	public void actionToWakenUp(WakenUpEvent e) {
		System.out.println("Feed a child"+" at "+e.getTime());
	}
	
}

class GrandFather implements WakenUpListener{
	public void actionToWakenUp(WakenUpEvent e) {
		System.out.println("Hug a child"+" at "+e.getTime());
	}
	
}

interface WakenUpListener{
	public void actionToWakenUp(WakenUpEvent e);
}

class PropertyMgr{
	private static Properties props = new Properties();
	static{
		try {
			props.load(test.class.getClassLoader()
					.getResourceAsStream("com/anyin/blog/observer.properties"));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	public static String getProperty(String key){
		return props.getProperty(key);
	}
}

public class test{
	public static void main(String[] args){

		Child c = new Child();
		
		String observer = PropertyMgr.getProperty("observers");
		
		String[] observers = observer.split(",");
		for(String s : observers){
			System.out.println(s);
			try {
				c.addWakenUpListener((WakenUpListener)(Class.forName(s).newInstance()));
				//c.addWakenUpListener((WakenUpListener)(Class.forName(s).newInstance()));
			} catch (InstantiationException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (ClassNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
//		Dad d = new Dad();
//		GrandFather gf = new GrandFather();
//		c.addWakenUpListener(d);
//		c.addWakenUpListener(gf);
		new Thread(c).start();
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -