Как использовать функцию как параметр java — Q&A Хекслет
2026-02-26 19:50 Diff
public static void superMethodReflection(Object object, Method method) throws Exception { int a = 10; int b = 20; int result = (int) method.invoke(object, a, b); System.out.println(result); } // вызов с передачей методов public class App { public static void main(String[] args) throws Exception { // передадим стандартный метод Method method = Integer.class.getDeclaredMethod("max", int.class, int.class); superMethodReflection(0, method); // => 20 method = Integer.class.getDeclaredMethod("sum", int.class, int.class); superMethodReflection(0, method); // => 30 // передадим собственный метод method = App.class.getDeclaredMethod("concate", int.class, int.class); superMethodReflection(new App(), method); // => 1020 } public static int concate(int a, int b) { return Integer.parseInt("" + a + b); } }