[英]Java InstantiationException
I made a class to implement an interface and was testing it using another class. 我制作了一个类来实现接口,并正在使用另一个类对其进行测试。
This is the class that I created. 这是我创建的类。
public class MyWeaponI implements WeaponI {
Random RAND = new Random();
private int maxDamage;
private String name;
public MyWeaponI(String name1){
maxDamage = 10;
name = name1;
}
@Override
public int getDamage() {
return RAND.nextInt(maxDamage)+1;
}
@Override
public int getMaxDamage() {
return maxDamage;
}
@Override
public String toString(){
return String.format("Weapon %s, damage=%d", name, maxDamage);
}
@Override
public void initFromString(String input) {
Scanner s = new Scanner(input);
s.useDelimiter("$n");
String pattern = "(\\w+)\\s*,\\s*(\\d)\\s*";
if(s.hasNext(pattern)){
MatchResult m = s.match();
maxDamage = Integer.parseInt(m.group(2));
name = m.group(1);
System.out.println(String.format("Weapon %s, damage=%d", m.group(1), Integer.parseInt(m.group(2))));
}
}
@Override
public String getName() {
return name;
}
}
This is the part of the tester that I get the error in. I just took out part of the tester to reduce the amount of code that I was posting. 这是导致错误的测试人员的一部分。我只是删除了一部分测试人员,以减少发布的代码量。 If you need the full tester I can change it.
如果您需要完整的测试仪,我可以进行更改。
Class warriorClass = null, weaponClass = null, diskClass = null;
for(int i=0;i<args.length;i++) {
Class c = Class.forName(args[i]);
if(WeaponI.class.isAssignableFrom(c))
weaponClass = c;
else if(WarriorI.class.isAssignableFrom(c))
warriorClass = c;
else if(DiskI.class.isAssignableFrom(c))
diskClass = c;
else
assert false : "Not a class name: "+args[i];
}
assert weaponClass != null : "You need to supply a weapon class";
WeaponI weapon = (WeaponI)weaponClass.newInstance();
testWeapon(weapon);
When I run the code, I get an InstatiationException on the line of the tester that starts with "WeaponI weapon" near the bottom. 当我运行代码时,在测试器的行上得到一个InstatiationException,该异常以底部附近的“ WeaponI武器”开头。 There are two other classes that that also need to be passed to the tester and I'm assuming that both of them will also have to same problem.
还有两个其他类也需要传递给测试人员,我假设它们两个也都将面临相同的问题。 I honestly have no idea how to fix the problem, so any help would be much appreciated.
老实说,我不知道如何解决该问题,因此我们将不胜感激。
Exception in thread "main" java.lang.InstantiationException: warriorsandweapons.MyWeaponI
at java.lang.Class.newInstance(Class.java:368)
at warriorsandweapons.Arena.main(Arena.java:308)
Java Result: 1
For Class.newInstance
to work it needs a default constructor. 为了使
Class.newInstance
工作,它需要一个默认的构造函数。 You can add a default constructor to MyWeapon
class and try. 您可以向
MyWeapon
类添加默认构造函数,然后尝试。
Quoting from javadoc for Class 从Javadoc报价为Class
InstantiationException - if this Class represents an abstract class, an interface, an array class, a primitive type, or void;
InstantiationException-如果此Class表示一个抽象类,一个接口,一个数组类,一个原始类型或void; or if the class has no nullary constructor;
或如果类没有空构造函数;或者 or if the instantiation fails for some other reason.
或者实例化由于其他原因而失败。
You're doing the following: 您正在执行以下操作:
WeaponI weapon = (WeaponI)weaponClass.newInstance();
On class warriorsandweapons.MyWeaponI
. 关于类
warriorsandweapons.MyWeaponI
。 This class has only one constructor, and it's a constructor that takes a single argument. 该类只有一个构造函数,并且它是一个带有单个参数的构造函数。
If you look at the Javadoc for Class.newInstance()
you see the following note: 如果查看
Class.newInstance()
的Javadoc, Class.newInstance()
看到以下注释:
The class is instantiated as if by a new expression with an empty argument list.
就像通过带有空参数列表的新表达式实例化该类。
So you can only use newInstance
if there is a constructor that doesn't take arguments. 因此,只有在有一个不带参数的构造函数时,才可以使用
newInstance
。 There is no such constructor on MyWeaponI
, that's why you get this exception. MyWeaponI
上没有这样的构造MyWeaponI
,这就是为什么会出现此异常的原因。
Instead, you can use java.lang.reflect.Constructor
to create the instance and pass arguments: 相反,可以使用
java.lang.reflect.Constructor
创建实例并传递参数:
Class<WeaponI> weaponClass;
// ...
Constructor<WeaponI> constructor = weaponClass.getConstructor(String.class);
WeaponI instance = constructor.newInstance(nameArgument);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.