这是构造函数重载:
public class Diagraph { public Diagraph(int n) { // Constructor pre } public Digraph(In in) { this(in.readInt()); // Calls the constructor above. int E = in.readInt(); for (int i = 0; i < E; i++) { int v = in.readInt(); int w = in.readInt(); addEdge(v, w); } }}您可以通过缺少返回类型来判断此代码是构造函数,而不是方法。这与
super()在构造函数的第一行中调用以初始化扩展类非常相似。您应该在构造函数的第一行中调用
this()(或的任何其他重载
this()),从而避免构造函数代码重复。
您也可以看看这篇文章:Java中的构造方法重载-最佳实践



