//匿名类
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;
}
}
*/
Java 匿名类示例
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.