码迷,mamicode.com
首页 > 编程语言 > 详细

java把函数作为参数传递

时间:2016-05-13 10:34:24      阅读:268      评论:0      收藏:0      [点我收藏+]

标签:

    public class Tool {
        public void a()// /方法a 
        {
            System.out.print("tool.a()...");
        }
        public void b()// 方法b
        {
            System.out.print("tool.b()...");
        }
    }

    public class Control {
        public void invoke(int flag) {
            User user = new User();
            try {
                switch (flag) {
                case 0:
                    user.use(Tool.class.getMethod("a", null));
                    break;
                default:
                    user.use(Tool.class.getMethod("b", null));
                    break;
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public class User {
        public void use(Method method) {
            Tool tool = new Tool();
            try {
                method.invoke(tool, null);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        public static void main(String[] args) {
            Control control = new Control();
            control.invoke(0);
        }
    }

 

java把函数作为参数传递

标签:

原文地址:http://www.cnblogs.com/destim/p/5485828.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!