标签:
---恢复内容开始---
内部类:
public class Inner{
public class Inner2{}
}
创建内部类对象 .new
public class Test {
int i=10;
public class A{
int a=100;
}
/*public A A(){
return new A();
}*/
public static void main(String[] args) {
Test t=new Test();
System.out.println(t.i);
Test.A ta=t.new A();//使用内部类,创建内部类对象
System.out.println(ta.a);
}
}
匿名内部类
public class Parcel7 {
public Contents contents() {
return new Contents() { // Insert a class definition
private int i = 11;
public int value() { return i; }
}; // Semicolon required in this case
}
public static void main(String[] args) {
Parcel7 p = new Parcel7();
Contents c = p.contents();
System.out.println(c.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();
}
}
---恢复内容结束---
标签:
原文地址:http://www.cnblogs.com/bulrush/p/5437926.html