我需要从类对象数组创建类的新实例,如下所示:
static Class[] spells = {Fireball.class, Iceball.class};
所以当我想调用火球时我应该能够做类似的事情
Spell Currentspell = new spells[0](posx, posy);
顺便说一下,火球术和冰球术是法术的子类。
我该怎么做呢?
谢谢您的问候。
Constructor constructor = spells[0].getConstructor(int.class, int.class);
Spell Currentspell = (Spell)constructor.newInstance(posx, posy);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)