[英]How to check if a java class contain a default constructor?
I want to check if a Java class contains a default constructor.我想检查 Java class 是否包含默认构造函数。
Case 1:情况1:
public class ClassWithConstructor {
//attributes...
//default constructor
public ClassWithConstructor(){}
}
Case 2:案例二:
public class ClassWithoutConstructor {
//attributes...
// no default constructor
}
In case 1, I want to print "ClassWithConstructor contains a default constructor."
在案例 1 中,我想打印
"ClassWithConstructor contains a default constructor."
In case 2, I want to print "ClassWithoutConstructor doesn't contain any default constructor"
.在情况 2 中,我想打印
"ClassWithoutConstructor doesn't contain any default constructor"
。
You can inspect your class via the Java Reflection API, there is a class called Constructor
(see http://docs.oracle.com/javase/7/docs/api/java/lang/reflect/package-frame.html ). 您可以通过Java Reflection API检查您的类,有一个名为
Constructor
的类(请参阅http://docs.oracle.com/javase/7/docs/api/java/lang/reflect/package-frame.html )。 Not sure, though, whether you can actually distinguish the Java default constructor and a parameterless one you defined on your own. 但是,不确定您是否能够真正区分Java默认构造函数和您自己定义的无参数构造函数。
You can't check this case. 你不能检查这种情况。 The class without constructors creates a default constructor with the same attributes that a
public ClassName(){}
, nevertheless you can check if the modifier or attributes change. 没有构造函数的类创建一个默认构造函数,其属性与
public ClassName(){}
,但是您可以检查修饰符是否更改。
If you debug this code, you will see that both Constructors[] have the same attributes except the clazz, they have the distinct name. 如果您调试此代码,您将看到两个Constructors []具有除clazz之外的相同属性,它们具有不同的名称。
import java.lang.reflect.Constructor;
import org.junit.Test;
public class ScriptBuilderTest {
@Test
public void test() {
Class<ObjectWithDeclaredConstructor> ObjectWithDC = ObjectWithDeclaredConstructor.class;
Class<ObjectWithoutDeclaredConstructor> ObjectWithoutDC = ObjectWithoutDeclaredConstructor.class;
Constructor<?>[] ctorsWithDC = ObjectWithDC.getDeclaredConstructors();
Constructor<?>[] ctorsWithoutDC = ObjectWithoutDC.getDeclaredConstructors();
System.out.println("end");
}
public class ObjectWithDeclaredConstructor{
public ObjectWithDeclaredConstructor(){}
}
public class ObjectWithoutDeclaredConstructor{ }
}
Case 1: In case 1, you don't have a default constructor.情况 1:在情况 1 中,您没有默认构造函数。 But in case 2, you have a default constructor.
但在情况 2 中,您有一个默认构造函数。 In case 1 the constructor is created by you, not by your compiler.
在情况 1 中,构造函数是由您创建的,而不是由您的编译器创建的。 In Java, the compiler is always responsible for creating a default constructor.
在 Java 中,编译器始终负责创建默认构造函数。 In case 1 the compiler code looks like below.
在案例 1 中,编译器代码如下所示。
public class ClassWithConstructor {
public ClassWithConstructor(){
super();//This code is generated by compiler
}
}
S0 super()
is generated by the compiler since the first line of the constructor should be this()
or super()
. S0
super()
由编译器生成,因为构造函数的第一行应该是this()
或super()
。 Since it does not have anything I created super()
.因为它没有我创建的任何东西
super()
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.