我遇到了一个困扰我的问题,它是关键字'super',我的测试代码是这样的:
package test;
public class Parent {
private String name;
public Parent(){
this.name = "parent";
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void showName(){
System.out.println(this.name);
}
}
public class Child extends Parent{
public Child(){
this.setName("Child");
}
public void showName(){
System.out.println(super.getClass().toString());
System.out.println(super.toString());
super.showName();
System.out.println(super.getName());
}
}
public class Test {
public static void main(String[] args) {
Child d = new Child();
d.showName();
}
}
Run Code Online (Sandbox Code Playgroud)
所以结果是这样的:
class test.Child
test.Child@2207d8bb
Child
Child …Run Code Online (Sandbox Code Playgroud)