所有答案都很好。
但是让我们用代码来谈论。以下代码片段将为您提供更清晰的理解。
PersonWithImplicitConstructor.java
public class PersonWithImplicitConstructor {
private int id;
private String name;
}
首先,我们需要编译.java
文件。
javac PersonWithImplicitConstructor.java
然后会生成class文件。
在该class文件上运行javap
命令,将会得到以下信息。
javap PersonWithImplicitConstructor.class
Compiled from "PersonWithImplicitConstructor.java"
public class PersonWithImplicitConstructor {
public PersonWithImplicitConstructor();
}
注意:如果您想获取更多信息,可以在javap
命令后使用-p
标志。
下一个Java文件将只有参数化构造函数。
PersonWithExplicitConstructor.java
public class PersonWithExplicitConstructor {
private int id;
private String name;
public PersonWithExplicitConstructor(int id, String name) {
this.id = id;
this.name = name;
}
}
使用javac编译PersonWithExplicitConstructor.java文件
使用javap查看PersonWithExplicitConstructor.class文件
Compiled from "PersonWithExplicitConstructor.java"
public class PersonWithExplicitConstructor {
public PersonWithExplicitConstructor(int, java.lang.String);
}
PersonWithBothConstructors.java
public class PersonWithBothConstructors {
private int id;
private String name;
public PersonWithBothConstructors() {
}
public PersonWithBothConstructors(int id, String name) {
this.id = id;
this.name = name;
}
}
使用javac编译PersonWithBothConstructors.java文件
使用javap查看PersonWithBothConstructors.class文件
Compiled from "PersonWithBothConstructors.java"
public class PersonWithBothConstructors {
public PersonWithBothConstructors();
public PersonWithBothConstructors(int, java.lang.String);
}