我有三个功能test1(),test2()并test3()用Java编写的.我想编写一些代码来迭代并将它们调用到所有不同的组合中.
例如
test1(),然后test2(),最后test3(),test2(),test1()最后test3()等是否有可能通过迭代器执行此操作,或者我必须手动执行此操作?
您可以使用反射来获取方法数组,然后遍历列表的每个排列并一次调用一个.看看Class.getDeclaredMethods()的JavaDoc.这是一个简单的例子:
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
public class SO {
public void test1() {
System.out.println("Running test1");
}
public void test2() {
System.out.println("Running test2");
}
public void test3() {
System.out.println("Running test3");
}
public void notATest() {
System.err.println("THIS IS NOT A TEST");
}
public static void main(String ... args) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException {
Class c = SO.class;
SO that = new SO();
Method[] methods = filter(c.getDeclaredMethods(), "test");
PermuteMethod permutations = new PermuteMethod(methods);
while(permutations.hasNext()) {
for(Method permutation: permutations.next()) {
permutation.invoke(that, null);
}
System.out.println("----");
}
}
private static Method[] filter(Method[] declaredMethods, String startsWith) {
List<Method> filtered = new ArrayList<>();
for(Method method : declaredMethods) {
if(method.getName().startsWith(startsWith)) {
filtered.add(method);
}
}
return filtered.toArray(new Method[filtered.size()]);
}
}
Run Code Online (Sandbox Code Playgroud)
这会产生follwoung输出(不是它从不调用notATest() method:
Running test1
Running test2
Running test3
----
Running test1
Running test3
Running test2
----
Running test2
Running test1
Running test3
----
Running test2
Running test3
Running test1
----
Running test3
Running test1
Running test2
----
Running test3
Running test2
Running test1
----
Run Code Online (Sandbox Code Playgroud)
这是使用此permute类的以下(修改)版本:
import java.lang.reflect.Array;
import java.lang.reflect.Method;
import java.util.Iterator;
import java.util.NoSuchElementException;
public class PermuteMethod implements Iterator<Method[]> {
private final int size;
private final Method[] elements; // copy of original 0 .. size-1
private final Method[] ar; // array for output, 0 .. size-1
private final int[] permutation; // perm of nums 1..size, perm[0]=0
private boolean next = true;
public PermuteMethod(Method[] e) {
size = e.length;
elements = new Method[size];
System.arraycopy(e, 0, elements, 0, size);
ar = new Method[size];
System.arraycopy(e, 0, ar, 0, size);
permutation = new int[size + 1];
for (int i = 0; i < size + 1; i++) {
permutation[i] = i;
}
}
private void formNextPermutation() {
for (int i = 0; i < size; i++) {
Array.set(ar, i, elements[permutation[i + 1] - 1]);
}
}
public boolean hasNext() {
return next;
}
public void remove() throws UnsupportedOperationException {
throw new UnsupportedOperationException();
}
private void swap(final int i, final int j) {
final int x = permutation[i];
permutation[i] = permutation[j];
permutation[j] = x;
}
public Method[] next() throws NoSuchElementException {
formNextPermutation(); // copy original elements
int i = size - 1;
while (permutation[i] > permutation[i + 1])
i--;
if (i == 0) {
next = false;
for (int j = 0; j < size + 1; j++) {
permutation[j] = j;
}
return ar;
}
int j = size;
while (permutation[i] > permutation[j])
j--;
swap(i, j);
int r = size;
int s = i + 1;
while (r > s) {
swap(r, s);
r--;
s++;
}
return ar;
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
155 次 |
| 最近记录: |