Java PASS方法作為參數(shù)我正在尋找一種方法,通過參考通過。我理解Java并不將方法作為參數(shù)傳遞,但是,我想得到另一種選擇。有人告訴我,接口是作為參數(shù)傳遞方法的替代方法,但我不明白接口如何通過引用充當(dāng)方法。如果我正確理解,接口只是一組抽象的方法,沒有定義。我不想發(fā)送每次都需要定義的接口,因為幾個不同的方法可以用相同的參數(shù)調(diào)用相同的方法。我想要完成的是類似的事情: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());
添加回答
舉報
0/150
提交
取消