Rol*_*nez 5 java super superclass
有一个简单的程序,在构造函数中,super()被调用而没有扩展到超类,我无法理解在这种情况下会做什么呢?
public class Student {
private String name;
private int rollNum;
Student(String name,int rollNum){
super(); //I can not understand why super keyword here.
this.name=name;
this.rollNum=rollNum;
}
public static void main(String[] args) {
Student s1 = new Student("A",1);
Student s2 = new Student("A",1);
System.out.println(s1.equals(s2));
}
}
Run Code Online (Sandbox Code Playgroud)
JB *_*zet 10
每个未明确扩展另一个类的类都会隐式扩展java.lang.Object.所以super()简单地调用Object的no-arg构造函数.
请注意,此显式调用是不必要的,因为编译器会为您添加它.super()当您想要使用参数调用超类构造函数时,只需要在构造函数中添加调用.
| 归档时间: |
|
| 查看次数: |
3993 次 |
| 最近记录: |