【发布时间】:2018-10-31 14:47:09
【问题描述】:
我有两个变量。并且应该创建两个构造函数。
private int size;
final private Class clazz;
第一:
public SomeConstr(int size) {
if (size <= 0) {
this.size = 0;
IllegalArgumentException argumentException = new IllegalArgumentException();
logger.log(Level.SEVERE, "", argumentException);
throw argumentException;
}
else
this.size = size;
this.clazz = Device.class;
}
}
第二:
public ComeConstrSecond(int size, Class clazz) {
this(size);
if (clazz == null || !Device.class.isAssignableFrom(clazz)) {
logger.log(Level.SEVERE, "");
throw new IllegalArgumentException();
}
this.clazz = clazz;
}
当我在第二个构造函数中初始化this.clazz = clazz 时,我遇到了have been assigned to 这样的问题。如果我必须使用this(size),clazz 的写入初始化有多正确?
【问题讨论】:
标签: java constructor dry