Inherit ExtendsTwo-super&this

package Test;

public class ExtendsTwo {
  public static void main(String[] args) {
    Son s=new Son();
    s.print();
  }
}
class Father{
  int num1=10;
  int num2=20;
}
class Son extends Father{
  int num2=30;
  public void print() {
    System.out.println(this.num1);//10 Because the subclass inherits the things of the parent class, it can be called with this, if the subclass has it, the subclass is called , otherwise call
    System.out.println(num2) of the parent class;//30 Call the nearest one first, which is actually equivalent to this.num2, but omits this
    System.out.println(super.num2);//20
  }
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325007241&siteId=291194637