ここでは、当社の新人研修受講者に向けて、Javaのサンプルコードを紹介しています。
匿名クラス(無名クラス)
以下のプログラムの結果を見て、なぜ、そうなるのか説明してください。
[プログラム1]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
public class Anonymous{ public static void selfIntoroduction( Object o ){ System.out.println( o.toString( ) ); } public static void main( String args[ ] ){ selfIntoroduction( new Object( ){ public String toString( ){ String selfIntro = "I am a cat. I have no name yet."; return selfIntro; } } ); } } |
さらに、匿名クラスを使わずに同じことをするにはどうすればいいですか?