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(); } }