JAVA foundation (keyword --super)

1, this can mean anything and super

  • this: represents the current object reference, who will call me, I who represents

  • super: reference to the current object on behalf of the parent class

 

2, using the difference between this and super

  • Call member variables

    • this. member variable call this class member variables, member variables can also call the parent class

    • super. member variables to call the parent class member variables

  • Call the constructor

    • the this (...) method call the constructor of the present

    •  super (...) calls the constructor of the parent class

 

3, call member methods

  • this. This method calls the members of the class member method, you can also call the parent's method

  • super. members call the method of the parent class members

        

class Demo4_Extends {

    public static void main(String[] args) {

        Son s = new Son();

        s.print();

    }

}

/*

* A:案例演示

    * a:不同名的变量

    * b:同名的变量

        子父类出现同名的变量只是在讲课中举例子有,在开发中是不会出现这种情况的

        子类继承父类就是为了使用父类的成员,那么如果定义了同名的成员变量没有意义了

*/





class Father {

    int num1 = 10;

    int num2 = 30;

}





class Son extends Father {

    int num2 = 20;

    // int num1 = 10;  --> 从父类中继承下来





    public void print() {

        System.out.println(this.num1);                //this既可以调用本类的,也可以调用父类的(本类没有的情况下)

        System.out.println(this.num2);                //就近原则,子类有就不用父类的了

        System.out.println(super.num2);

        

    }

}

 

Guess you like

Origin blog.csdn.net/Cricket_7/article/details/92061022
Recommended