在Java中使用反射

在Java中使用反射,java,reflection,Java,Reflection,我需要一些关于反射的帮助,因为我不能让我的代码按照我想要的方式工作 我有以下资料: nrThreads = Utilities.getNumberOfThreads(filePath, propertiesFile); testName = Utilities.getTestName(filePath, propertiesFile); System.out.println(Utilities.nowDate()); System.out.println("Inserting...");

我需要一些关于反射的帮助,因为我不能让我的代码按照我想要的方式工作

我有以下资料:

nrThreads = Utilities.getNumberOfThreads(filePath, propertiesFile);
testName = Utilities.getTestName(filePath, propertiesFile);  
System.out.println(Utilities.nowDate());
System.out.println("Inserting...");

switch (testName)
{
case "InsertAndCommit":
      final InsertAndCommit[] threads = new InsertAndCommit[nrThreads];
      for (int i = 0; i < nrThreads; i++) {
        threads[i] = new InsertAndCommit();
        threads[i].start();
      }                         
      break;            
case "CommitAfterAllInserts":
      final CommitAfterAllInserts[] threads1 = new CommitAfterAllInserts[nrThreads];
      for (int i = 0; i < nrThreads; i++) {
        threads1[i] = new CommitAfterAllInserts();
        threads1[i].start();
      }
      break;
      default: break;
}
 Class<?> clazz = Class.forName(testName);
 Constructor<?> ctor = clazz.getConstructor(String.class);
 final Object[] obj = (Object[]) ctor.newInstance(); //this line isn't right, I need to declare the "threads" array (equivalent to: final InsertAndCommit[] threads = new InsertAndCommit[nrThreads];)

            for (int i = 0; i < nrThreads; i++) {
                //In this line I need to declare a new "generic constructor" with reflection (equivalent to threads[i] = new InsertAndCommit();) 
                threads[i].start();
            }
nrThreads=Utilities.getNumberOfThreads(文件路径,属性文件);
testName=Utilities.getTestName(文件路径,属性文件);
System.out.println(Utilities.nowDate());
System.out.println(“插入…”);
开关(testName)
{
案例“InsertAndCommit”:
final InsertAndCommit[]线程=新InsertAndCommit[nrThreads];
对于(int i=0;i
如您所见,我正在重复此开关/机箱中的代码。我知道我可以使用反射来完成这段代码,但我似乎做得不对

我已经做了以下工作:

nrThreads = Utilities.getNumberOfThreads(filePath, propertiesFile);
testName = Utilities.getTestName(filePath, propertiesFile);  
System.out.println(Utilities.nowDate());
System.out.println("Inserting...");

switch (testName)
{
case "InsertAndCommit":
      final InsertAndCommit[] threads = new InsertAndCommit[nrThreads];
      for (int i = 0; i < nrThreads; i++) {
        threads[i] = new InsertAndCommit();
        threads[i].start();
      }                         
      break;            
case "CommitAfterAllInserts":
      final CommitAfterAllInserts[] threads1 = new CommitAfterAllInserts[nrThreads];
      for (int i = 0; i < nrThreads; i++) {
        threads1[i] = new CommitAfterAllInserts();
        threads1[i].start();
      }
      break;
      default: break;
}
 Class<?> clazz = Class.forName(testName);
 Constructor<?> ctor = clazz.getConstructor(String.class);
 final Object[] obj = (Object[]) ctor.newInstance(); //this line isn't right, I need to declare the "threads" array (equivalent to: final InsertAndCommit[] threads = new InsertAndCommit[nrThreads];)

            for (int i = 0; i < nrThreads; i++) {
                //In this line I need to declare a new "generic constructor" with reflection (equivalent to threads[i] = new InsertAndCommit();) 
                threads[i].start();
            }
Class clazz=Class.forName(testName);
Constructor=clazz.getConstructor(String.class);
最终对象[]obj=(对象[])ctor.newInstance()//这一行不正确,我需要声明“threads”数组(相当于:final InsertAndCommit[]threads=new InsertAndCommit[nrThreads];)
对于(int i=0;i
我已经读了很多关于反思的书,但我似乎不能正确理解这一点,你能帮我吗

在这一行中,我需要声明一个新的带有反射的“泛型构造函数”(相当于
threads[I]=newinsertandcommit();

如果使用泛型,则不必通过反射来实现这一点,因为不需要显式地使用构造函数对象(尽管
Class.newInstance()
Array.newInstance()
方法是Java反射API的一部分)

因为您有
,而且两个类都有无参数构造函数,所以可以调用
clazz.newInstance()
来创建一个新对象,如下所示:

public <T extends Thread> T[] makeArray(Class<T> clazz, int n) throws Exception {
    T[] res = (T[]) Array.newInstance(clazz, n);
    for (int i = 0 ; i < n ; i++) {
        res[i] = clazz.newInstance();
        res[i].start();
    }
    return res;
}
public T[]makeArray(类clazz,int n)引发异常{
T[]res=(T[])Array.newInstance(clazz,n);
对于(int i=0;i

.

我认为您应该依赖这样一个事实:您的两个类实际上都是
线程的子类(我假设您在这两种情况下都使用
start()

  • 您可以创建
    Thread[]
    类型的数组,并将
    Thread
    子类的任何对象分配给它
  • 您不需要查找构造函数,因为可以直接从类对象调用无参数构造函数
  • 构造函数总是为您提供单个对象,而不是对象数组。因此,您应该只在循环中使用它,创建每个单独的线程,而不是创建数组
因此,缺少的部分是:

Class<? extends Thread> clazz = (Class<? extends Thread>) Class.forName(testName);
Thread[] threads = new Thread[nrThreads];
for ( int i = 0; i < nrThreads; i++ ) {
    threads[i] = clazz.newInstance();
    threads[i].start();
}

ClassIt只有在
InsertAndCommit
CommitAfterAllInserts
都实现相同的接口或扩展相同的类的情况下,才有意义使用反射。是这样吗?是的,它们都实现了可运行接口如果我的类不是从线程扩展而是实现了可运行接口怎么办?@SaintLike使用
extend Runnable
。如果我的类不是从线程扩展而是实现了可运行接口怎么办?那么你可以使用
Runnable[]runnables=new Runnable[nrThreads]
。当然,在这种情况下,您将无法直接在对象上运行
start
。如果你愿意,我可以扩展我关于这一点的答案。无需指定,反正这很好;)