this 和 super 的区别:this, 先从本类找属性和方法,本类找不到再从父类找。super, 从父类找。
this 和 super 都可以调用构造方法,所以this() 和 super() 不可以同时出现。
public class Person {
}
public class Student extends Person {
    String name;
    public Student(String name) {
        super();
//        this();
    }
    public Student() {}
}


class FatherClass {
    public int value;
    public void f() {
        value = 100;
        System.out.println("FatherClass,value = " + value);
    }
}

class ChildClass extends FatherClass {
    public int value;
    public void f() {
        super.f();
        value = 200;
        System.out.println("ChildClass,value = " + value);
        System.out.println(value);
        System.out.println(super.value);
    }
}

public class Demo {
    public static void main(String[] args) {
        ChildClass cc = new ChildClass();
        cc.f();
    }
}
View Code

相关文章:

  • 2022-01-18
  • 2022-02-14
  • 2022-12-23
  • 2021-06-19
  • 2021-10-19
  • 2021-12-30
猜你喜欢
  • 2021-08-24
  • 2021-07-04
  • 2021-09-27
  • 2022-01-07
  • 2022-01-07
相关资源
相似解决方案