我刚刚使用了 Eclipse 的“使用字段生成构造函数”功能,它为我提供了以下构造函数:
public Credentials(String userName, String password) {
super();
this.userName = userName;
this.password = password;
}
Credentials
类没有显式扩展另一个类,因此我猜它扩展了 Object
。
super();
调用有何用处?
最佳答案
如果您将构造函数定义为:
public Credentials(String userName, String password) {
this.userName = userName;
this.password = password;
}
然后编译器无论如何都会添加对父类(super class)构造函数的隐式调用,因此这不会对编译后的代码产生任何差异。
The Credentials class does not explicitly extend another class so it extends Object I guess. What is the super(); call good for?
但是您的类隐式扩展了 Object
,因此将调用 Object()
。
关于java - 在构造函数中调用 super(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22374323/