为了账号安全,请及时绑定邮箱和手机立即绑定

Java PASS方法作为参数

Java PASS方法作为参数

温温酱 2019-06-18 16:20:13
Java PASS方法作为参数我正在寻找一种方法,通过参考通过。我理解Java并不将方法作为参数传递,但是,我想得到另一种选择。有人告诉我,接口是作为参数传递方法的替代方法,但我不明白接口如何通过引用充当方法。如果我正确理解,接口只是一组抽象的方法,没有定义。我不想发送每次都需要定义的接口,因为几个不同的方法可以用相同的参数调用相同的方法。我想要完成的是类似的事情:public void setAllComponents(Component[] myComponentArray, Method myMethod) {     for (Component leaf : myComponentArray) {         if (leaf instanceof Container) { //recursive call if Container             Container node = (Container) leaf;             setAllComponents(node.getComponents(), myMethod);         } //end if node         myMethod(leaf);     } //end looping through components}援引如下:setAllComponents(this.getComponents(), changeColor());setAllComponents(this.getComponents(), changeSize());
查看完整描述

3 回答

?
白猪掌柜的

TA贡献1893条经验 获得超10个赞

使用java.lang.reflect.Method对象和调用invoke


查看完整回答
反对 回复 2019-06-18
  • 3 回答
  • 0 关注
  • 847 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号