Java 匿名類示例

//匿名類
interface Contents{
	int value();
}

public class Parcel4 {
	public Contents cont(){
		return new Contents(){
			private int i = 11;
			public int value(){
				return i;
			}
		};//此處需要分號,表示匿名類的結束
	}
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Parcel4 p = new Parcel4();
		Contents c = p.cont();
		System.out.println("" + c.value());
	}
}
/*
 class MyContents implements Contents{
 	private int i = 11;
 	public int value(){
 		return i;
 	}
 }
*/

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章