小编Ric*_*hen的帖子

如何在java中理解super

我遇到了一个困扰我的问题,它是关键字'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)

java properties super

0
推荐指数
1
解决办法
684
查看次数

标签 统计

java ×1

properties ×1

super ×1