Java 我可以将参数传递给动态创建的类吗?

Java 我可以将参数传递给动态创建的类吗?,java,parameter-passing,processing,classloader,urlclassloader,Java,Parameter Passing,Processing,Classloader,Urlclassloader,我在运行时成功地从一个外部MyClass.class文件加载了一个类,并且能够调用它的方法,假设我知道它们的名称,我就是这么做的 我遇到的问题是,我不知道如何将参数传递给我正在加载的类的构造函数 我如何修改它以将参数传递给MyClass的构造函数?另外,如何访问MyClass的公共变量:infoToAccess 这是我正在处理的文件。(请记住,ClassLoaderExample.pde是为处理而编写的,但除了sketchPath(“”和缺少main函数之外,它是相同的 ClassLoaderE

我在运行时成功地从一个外部
MyClass.class
文件加载了一个类,并且能够调用它的方法,假设我知道它们的名称,我就是这么做的

我遇到的问题是,我不知道如何将参数传递给我正在加载的类的构造函数

我如何修改它以将参数传递给
MyClass
的构造函数?另外,如何访问
MyClass
的公共变量:
infoToAccess

这是我正在处理的文件。(请记住,ClassLoaderExample.pde是为处理而编写的,但除了
sketchPath(“”
和缺少
main
函数之外,它是相同的

ClassLoaderExample.pde:加载类的主文件:
感谢您的帮助!

正如我在评论中所写的链接中提到的,您可以在此调用中传递参数

updateMethod=loadedClass.getDeclaredMethod(“更新”,参数在此处)


然后,您应该能够使用参数调用该方法。

如果您想将参数传递给构造函数,而不能使用
getInstance()
,则必须获取构造函数对象并在那里传递参数

例如:

Constructor constructor = aClass.getConstructor(new Class[]{String.class});
MyObject myObject = (MyObject) constructor.newInstance("constructor-arg1");

请参见此处的教程:

您可以使用加载的类来获取构造函数 您需要使用它来创建新实例

import java.lang.reflect.Constructor;

...

Class<?> loadedClass = classLoader.loadClass("tmp."+className);
try {
  Constructor<?> ctor=loadedClass.getConstructor(int.class);
  ctor.newInstance(42);
  ...
}
...
import java.lang.reflect.Constructor;
...
Class loadedClass=classLoader.loadClass(“tmp.+className”);
试一试{
构造函数ctor=loadedClass.getConstructor(int.class);
新实例(42);;
...
}
...

对于您询问的两件事,您都需要使用反射:

Class<?> loadedClass = classLoader.loadClass("tmp." + className);
Object oInstance = null;
try {
    Constructor<?> c = loadedClass.getConstructor(Integer.class);
    oInstance = c.newInstance(10);
} catch (InstantiationException | IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
    //Handle the possibility that you cannot access this constructor
}

你可能想看看这里,我想你被否决了,因为这不是我要问的问题,但这只是为了让我了解,所以我很感激能得到的任何新信息。谢谢!我在这里加入了你的贡献:谢谢你提到这个名字:)当我直接实现您的解决方案时,
getConstructor
行抛出以下错误:
java.lang.NoSuchMethodException:tmp.MyClass.(java.lang.Integer)
但是,如果我用
int.class
替换
Integer.class
,它会工作。然而,该领域的例子工作得很好。谢谢!我加入了你的贡献。
int.class
对我来说是一个新的课程-你每天都能学到一些东西:)这非常有效。谢谢我吸收了你的贡献。
import java.lang.reflect.Constructor;

...

Class<?> loadedClass = classLoader.loadClass("tmp."+className);
try {
  Constructor<?> ctor=loadedClass.getConstructor(int.class);
  ctor.newInstance(42);
  ...
}
...
Class<?> loadedClass = classLoader.loadClass("tmp." + className);
Object oInstance = null;
try {
    Constructor<?> c = loadedClass.getConstructor(Integer.class);
    oInstance = c.newInstance(10);
} catch (InstantiationException | IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
    //Handle the possibility that you cannot access this constructor
}
try {
    Field field = loadedClass.getField("infoToAccess");
    int fieldValue = field.getInt(oInstance);
} catch (NoSuchFieldException | IllegalAccessException e) {
    //Handle the possibility that you cannot access this field
}