反射中用Constructor获取构造函数的名称时,使用getName()方法时,在不同的编译环境下返回不同结果,可能会返回包+类的名称,而不是simpleName
import java.lang.reflect.Constructor;
public class GetName {
public static void main(String[] args) {
Constructor[] constructors = SampleClass.class.getDeclaredConstructors();
for (Constructor constructor : constructors) {
System.out.println("Constructor: " + constructor.getName());
}
}
}
class SampleClass {
private String sampleField;
public SampleClass() throws ArrayIndexOutOfBoundsException {
}
private SampleClass(String sampleField){
this.sampleField = sampleField;
}
public String getSampleField() {
return sampleField;
}
public void setSampleField(String sampleField) {
this.sampleField = sampleField;
}
}
Constructor: com.pro.domain.SampleClass
Constructor: com.pro.domain.SampleClass
Constructor: SampleClass
Constructor: SampleClass