クレイジーjava学習ノート1002---コマンドモード
5230 ワード
- //
- interface Command
- {
- // process “ ”
- void process(int[] target);
- }
- class ProcessArray
- {
- public void process(int[] target,Command cmd)
- {
- cmd.process(target);
- }
- }
- class PrintCommand implements Command
- {
- public void process(int[] target)
- {
- for(int tmp :target)
- {
- System.out.println(" :"+tmp);
- }
- }
- }
- class AddCommand implements Command
- {
- public void process(int[] target)
- {
- int sum = 0;
- for(int tmp : target)
- {
- sum += tmp;
- }
- System.out.println(" :"+sum);
- }
- }
-
- public class TestCommand
- {
- public static void main(String[] args)
- {
- ProcessArray pa = new ProcessArray();
- int[] target = {3,-4,6,4};
- // , PrintCommand
- pa.process(target,new PrintCommand());
- System.out.println("---------------------");
- // , AddCommand
- pa.process(target,new AddCommand());
- ; }
- }
- // process “ ”
本文は“javaプログラム猿のブログ”のブログから出て、転載して作者と連絡してください!