24.Java_KeyWord 进阶 super this


public class SuperClass {
    int num = 10;

    public void method(){
        System.out.println("父类方法");
    }
}

public class SubClass extends SuperClass{
    int num = 20;

    @Override
    public void method(){
        super.method();
        System.out.println("子类方法");
    }

    public void show(){
        int num = 30;
        System.out.println(num);// 30
        System.out.println(this.num);// 20
        System.out.println(super.num);// 10
    }
}

public class Demo01Extends {
    public static void main(String[] args) {
        SubClass sub = new SubClass();
        sub.show();
        sub.method();
    }
}

内存图

1_Java_super_this_memory


Author: Wolfwotz
Reprint policy: All articles in this blog are used except for special statements CC BY 4.0 reprint polocy. If reproduced, please indicate source Wolfwotz !
  TOC