1. 程式人生 > >java中Super到底是什麼意思?必須舉例說明!

java中Super到底是什麼意思?必須舉例說明!

馬克-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