Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1073665
  • 博文数量: 252
  • 博客积分: 4561
  • 博客等级: 上校
  • 技术积分: 2833
  • 用 户 组: 普通用户
  • 注册时间: 2008-03-15 08:23
文章分类

全部博文(252)

文章存档

2015年(2)

2014年(1)

2013年(1)

2012年(16)

2011年(42)

2010年(67)

2009年(87)

2008年(36)

分类:

2009-07-06 08:55:55

public class Command1 extends Command
{
        public void handle()
        {
                System.out.println("Command1");
        }
}
public class Command2 extends Command
{
        public void handle()
        {
                System.out.println("Command2");
        }
}
public abstract class Command
{
        public abstract void handle();
}
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;
import java.util.Map.Entry;
import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.io.InputStreamReader;

public class Handle
{
        public static void main(String args[]) throws Exception
        {
                HashMap<String, Command> hm = new HashMap<String, Command>();

                Command command1 = new Command1();
                Command command2 = new Command2();

                hm.put("command1", command1);
                hm.put("command2", command2);

                BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));

                while (true)
                {
                        String str = reader.readLine();
                        if (str == null)
                                break;
                        else
                        {
                                Set<Entry<String, Command>> hsm = hm.entrySet();
                                Iterator<Entry<String, Command>> t = hsm.iterator();

                                while (t.hasNext())
                                {
                                        Entry e = t.next();
                                        if (str.equals(e.getKey()))
                                        {
                                                ((Command)e.getValue()).handle();
                                                break;
                                        }
                                }
                        }
                }
                reader.close();
        }
}

Handle: Handle.java
        javac $^

clean:
        rm -f *.class

install:
        java Handle

阅读(2211) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~