Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/349.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java-如何用反射实例化内部类?_Java_Android_Reflection - Fatal编程技术网

Java-如何用反射实例化内部类?

Java-如何用反射实例化内部类?,java,android,reflection,Java,Android,Reflection,我在使用反射实例化内部类时遇到了一些问题。这里有一个例子 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 grassNam

我在使用反射实例化内部类时遇到了一些问题。这里有一个例子

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
        }

    }

}
我只想得到构造函数或者只是实例化内部类。我的代码到目前为止

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();
CowManager CowManager=(CowManager)this.getSystemService(Context.COW\u服务);
最终类MainCowClass=Class.forName(cowManager.getClass().getName());
最终类[]howManyCows=MainCowClass.getDeclaredClasses();
类getCow=null;
for(int i=0;i

到目前为止,我似乎无法在cow中找到ox的构造函数

您需要首先通过执行以下操作来访问内部类

// 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);

您需要首先通过如下操作访问内部类

// 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);

我得到java.lang.InstanceException:class com.sample.Cow在创建cowClassInstance时没有零参数构造函数。我还想补充一点,为什么我必须创建一个新实例,而我已经创建了一个新实例
CowManager CowManager=(CowManager)this.getSystemService(Context.Cow_服务)?这是否意味着我必须创建一个新实例?或者用您的代码替换代码?
无零参数
错误意味着您没有传入初始化构造函数所需的参数。我在评论中提到了这一点。不,如果您已经拥有该类的实例,则不需要创建新实例。我得到java.lang.InstanceException:class com.sample.Cow在创建cowClassInstance时没有零参数构造函数。我还想添加,既然我已经创建了一个新实例
CowManager CowManager=(CowManager)this.getSystemService(Context.COW\u SERVICE),为什么还要创建一个新实例?这是否意味着我必须创建一个新实例?或者用您的代码替换代码?
无零参数
错误意味着您没有传入初始化构造函数所需的参数。我在评论中提到了这一点。不,如果你已经有了这个类的实例,那么你不需要创建一个新的实例。