next up previous
Next: 6.2 superコンストラクタ Up: 6 親クラスの参照 Previous: 6 親クラスの参照

6.1 super変数


class That {
    protected String nm() {
        return "That";
    }
}

class More extends That {
    protected String nm() {
        return "More";
    }
    protected void printNM() {
        That sref = (That) this;
        System.out.println("this.nm() = " + this.nm());
        System.out.println("sref.nm() = " + sref.nm());
        System.out.println("super.nm() = " + super.nm());        
    }        
    public static void main(String argv[]) {
        (new More()).printNM();
    }
}
という具合にMore.javaを定義し,実行すると,

% java More
this.nm() = More
sref.nm() = More
super.nm() = That
となる. 以下は, 何が表示されるか示せ.

class That {
    protected String nm() {
        return "That";
    }
}

class More extends That {
    protected String nm() {
        return "More";
    }
    protected void printNM() {
      That sref = (That) this;

      System.out.println("this.nm() = " + this.nm());
      System.out.println("this.nm() = " + sref.nm());        
      System.out.println("this.nm() = " + super.nm());
    }
}

class Super {
    static void main(String str[]) {
        More m = new More();
        m.printNM();
    }
}


generated through LaTeX2HTML. M.Inaba 平成18年5月7日