日期:2014-05-20 浏览次数:20562 次
public interface Contents { int value(); } public class Parcel7b { //这是一段普通代码 class MyContents implements Contents { private int i = 11; public int value() { return i; } } public Contents contents() { return new MyContents(); } public static void main(String[] args) { Parcel7b p = new Parcel7b(); Contents c = p.contents(); } }
public class Parcel7 { //这是上段普通代码的匿名类形式 public Contents contents() { return new Contents() { private int i = 11; public int value() { return i; } }; } public static void main(String[] args) { Parcel7 p = new Parcel7(); Contents c = p.contents(); } }
public class Wrapping { private int i; public Wrapping(int x) { i = x; } public int value() { return i; } } public class Parcel8 { //这是基类带构造器的匿名类形式 public Wrapping wrapping(int x) { return new Wrapping(x) { public int value() { return super.value() * 47; } }; } public static void main(String[] args) { Parcel8 p = new Parcel8(); Wrapping w = p.wrapping(10); System.out.println(w + " "); } }