Java - 如何用反射实例化内部class?
Java - How to instantiate inner class with reflection?
我一直在使用反射实例化内部 class 时遇到问题。这是一个例子。
public final class Cow {
public Cow (Bufallo flying, Horse swimming, int cowID, int numCows) {
//This is where the part I really dont know what the cow is doing
}
public Bull eatGrass(String grassName, AngusCattle farmName, JerseyCattle farmName){
Ox newBreed = new Ox(australiaFarm, somewhereOutThere);
//Something that has to do with cow eating grass
return Bull;
}
private static final class Ox extends BigHorns {
public Ox (AngusCattle farmName, ChianinaOx farmName) {
//Something about mating
}
}
}
我想要的只是获取构造函数或只是实例化内部 class。到目前为止我的代码...
CowManager cowManager = (CowManager) this.getSystemService(Context.COW_SERVICE);
final Class MainCowClass = Class.forName(cowManager.getClass().getName());
final Class[] howManyCows = MainCowClass.getDeclaredClasses();
Class getCow = null;
for (int i=0; i < howManyCows.length; i++) {
if (! howManyCows[i].getName().equals("Cow$Ox")) {
continue;
}
getCow = Class.forName(howManyCows[i].getName());
}
Constructor createCow = getCow.getDeclaredConstructor();
目前我似乎无法在牛里面找到牛的构造函数
您需要先访问内部 class,方法如下
// Straightforward parent class initializing
Class<?> cowClass = Class.forName("com.sample.Cow");
Object cowClassInstance = cowClass.newInstance();
// attempt to find the inner class
Class<?> oxClass = Class.forName("com.sample.Cow$Ox");
// Now find the instance of the inner class, you may need to pass in arguments for the constructor
Constructor<?> oxClassContructor = oxClass.getDeclaredConstructor(cowClass);
// initialize the inner instance using the parent class's (cow's) instance
Object oxClassInstance = oxClassContructor.newInstance(cowClassInstance);
编辑
构造函数应该如下所示
// define the type of args that the constructor requires.
oxClass.getDeclaredConstructor(com.sample.AngusCattle.class, com.sample.ChianinaOx.class);
// now init the constructor with the required args.
Object oxClassInstance = oxClassContructor.newInstance(cowClassInstance, angusCattleClassInstance, chianinaOxClassInstance);
我一直在使用反射实例化内部 class 时遇到问题。这是一个例子。
public final class Cow {
public Cow (Bufallo flying, Horse swimming, int cowID, int numCows) {
//This is where the part I really dont know what the cow is doing
}
public Bull eatGrass(String grassName, AngusCattle farmName, JerseyCattle farmName){
Ox newBreed = new Ox(australiaFarm, somewhereOutThere);
//Something that has to do with cow eating grass
return Bull;
}
private static final class Ox extends BigHorns {
public Ox (AngusCattle farmName, ChianinaOx farmName) {
//Something about mating
}
}
}
我想要的只是获取构造函数或只是实例化内部 class。到目前为止我的代码...
CowManager cowManager = (CowManager) this.getSystemService(Context.COW_SERVICE);
final Class MainCowClass = Class.forName(cowManager.getClass().getName());
final Class[] howManyCows = MainCowClass.getDeclaredClasses();
Class getCow = null;
for (int i=0; i < howManyCows.length; i++) {
if (! howManyCows[i].getName().equals("Cow$Ox")) {
continue;
}
getCow = Class.forName(howManyCows[i].getName());
}
Constructor createCow = getCow.getDeclaredConstructor();
目前我似乎无法在牛里面找到牛的构造函数
您需要先访问内部 class,方法如下
// Straightforward parent class initializing
Class<?> cowClass = Class.forName("com.sample.Cow");
Object cowClassInstance = cowClass.newInstance();
// attempt to find the inner class
Class<?> oxClass = Class.forName("com.sample.Cow$Ox");
// Now find the instance of the inner class, you may need to pass in arguments for the constructor
Constructor<?> oxClassContructor = oxClass.getDeclaredConstructor(cowClass);
// initialize the inner instance using the parent class's (cow's) instance
Object oxClassInstance = oxClassContructor.newInstance(cowClassInstance);
编辑
构造函数应该如下所示
// define the type of args that the constructor requires.
oxClass.getDeclaredConstructor(com.sample.AngusCattle.class, com.sample.ChianinaOx.class);
// now init the constructor with the required args.
Object oxClassInstance = oxClassContructor.newInstance(cowClassInstance, angusCattleClassInstance, chianinaOxClassInstance);