如何创建 class 名称列表并遍历其中的静态方法?
How to create a list of class names and iterate throught the static methoc in it?
我有 类 的列表:
List<Class<?>> = Array.asList(ClassA, ClassB, ClassC, ClassD);
他们每个人都有一个静态方法staticMethod(byte[] bytes)
如何遍历 类 的这个列表并调用 staticMethod?
像静态方法的 lambda 表达式一样表示您的列表。
List<Consumer<byte[]>> functionsList = Arrays.asList(ClassA::staticMethod, ClassB::staticMethod);
byte[] bytes = new byte[] {1, 2, 3, 4, 5};
functionsList.forEach(consumer -> consumer.accept(bytes));
完整代码:
import java.util.Arrays;
import java.util.List;
import java.util.function.Consumer;
public class Main {
public static void main(String args[]){
List<Consumer<byte[]>> functionsList = Arrays.asList(ClassA::staticMethod, ClassB::staticMethod);
byte[] bytes = new byte[] {1, 2, 3, 4, 5};
functionsList.forEach(consumer -> consumer.accept(bytes));
}
}
public class ClassA {
public static void staticMethod(byte[] bytes) {
System.out.println("ClassA.staticMethod(byte[] bytes)" + Arrays.toString(bytes));
}
}
public class ClassB {
public static void staticMethod(byte[] bytes) {
System.out.println("ClassB.staticMethod(byte[] bytes)" + Arrays.toString(bytes));
}
}
输出:
ClassA.staticMethod(byte[] bytes)[1, 2, 3, 4, 5]
ClassB.staticMethod(byte[] bytes)[1, 2, 3, 4, 5]
编辑:
具有 return 值的方法示例:
import java.util.Arrays;
import java.util.List;
import java.util.function.Function;
public class Main {
public static void main(String args[]){
byte[] bytes = new byte[] {1, 2, 3, 4, 5};
List<Function<byte[], String>> functionsList = Arrays.asList(ClassA::staticMethodWithReturn, ClassB::staticMethodWithReturn);
functionsList.forEach(function -> {
String returnValue = function.apply(bytes);
}
);
}
}
public class ClassA {
public static String staticMethodWithReturn(byte[] bytes) {
return "";
}
}
public class ClassB {
public static String staticMethodWithReturn(byte[] bytes) {
return "";
}
}
我有 类 的列表:
List<Class<?>> = Array.asList(ClassA, ClassB, ClassC, ClassD);
他们每个人都有一个静态方法staticMethod(byte[] bytes)
如何遍历 类 的这个列表并调用 staticMethod?
像静态方法的 lambda 表达式一样表示您的列表。
List<Consumer<byte[]>> functionsList = Arrays.asList(ClassA::staticMethod, ClassB::staticMethod);
byte[] bytes = new byte[] {1, 2, 3, 4, 5};
functionsList.forEach(consumer -> consumer.accept(bytes));
完整代码:
import java.util.Arrays;
import java.util.List;
import java.util.function.Consumer;
public class Main {
public static void main(String args[]){
List<Consumer<byte[]>> functionsList = Arrays.asList(ClassA::staticMethod, ClassB::staticMethod);
byte[] bytes = new byte[] {1, 2, 3, 4, 5};
functionsList.forEach(consumer -> consumer.accept(bytes));
}
}
public class ClassA {
public static void staticMethod(byte[] bytes) {
System.out.println("ClassA.staticMethod(byte[] bytes)" + Arrays.toString(bytes));
}
}
public class ClassB {
public static void staticMethod(byte[] bytes) {
System.out.println("ClassB.staticMethod(byte[] bytes)" + Arrays.toString(bytes));
}
}
输出:
ClassA.staticMethod(byte[] bytes)[1, 2, 3, 4, 5]
ClassB.staticMethod(byte[] bytes)[1, 2, 3, 4, 5]
编辑: 具有 return 值的方法示例:
import java.util.Arrays;
import java.util.List;
import java.util.function.Function;
public class Main {
public static void main(String args[]){
byte[] bytes = new byte[] {1, 2, 3, 4, 5};
List<Function<byte[], String>> functionsList = Arrays.asList(ClassA::staticMethodWithReturn, ClassB::staticMethodWithReturn);
functionsList.forEach(function -> {
String returnValue = function.apply(bytes);
}
);
}
}
public class ClassA {
public static String staticMethodWithReturn(byte[] bytes) {
return "";
}
}
public class ClassB {
public static String staticMethodWithReturn(byte[] bytes) {
return "";
}
}