java中Super到底是什么意思?必须举例说明!

时间:2022-05-15 11:22:56
马克-to-win,Super是一个参考(或说指针)指向他紧邻的父类(见下面的例子)。Super is a reference of its neighbour superclass
So Use super to call superclass’s constructor
用super可以指向被隐藏的父类的同名成员。Use super to call superclass’s members that has been hidden by a member of a subclass.
3.1 super指向父类的成员
注 意: 下例中:子类和父类都有i,我们一共有两个i,用super可以指向前一个父类的i。 note that: in the following case, subclass and super class both have a i, so altogether they have two i.

例1.3.1
class AMark_to_win {
    int i;
}

class B extends AMark_to_win {
    int i;

    public B(int x, int y) {
        super.i = x;//AMark_to_win 的 i被赋值
        i = y;//B的i被赋值
    }

    public void show() {
        System.out.println("i in superclass: " + super.i);
        System.out.println("i in subclass: " + i);
    }
}

public class Test {
    public static void main(String[] args) {
        B b = new B(2, 3);。。。。。。。。。。

详情请见: http://www.mark-to-win.com/JavaBeginner/JavaBeginner3_web.html#Super