Inner Class
public class Main {
public static void main(String[] args) {
Outer outer = new Outer("Nested");
Outer.Inner inner = outer.new Inner();
inner.hello();
}
}
class Outer {
private final String name;
Outer(String name) {
this.name = name;
}
class Inner {
void hello() {
System.out.println("Hello, " + Outer.this.name);
}
}
}
Anonymous Class匿名
public class Main {
public static void main(String[] args) {
Outer outer = new Outer("Nested");
outer.asyncHello();
}
}
class Outer {
private final String name;
public Outer(String name) {
this.name = name;
}
void asyncHello() {
Runnable r = new Runnable() {
@Override
public void run() {
System.out.println("Hello," + Outer.this.name);
}
};
new Thread(r).start();
}
}
静态内部类
public class Main {
public static void main(String[] args) {
Outer.StaticNested sn = new Outer.StaticNested();
sn.hello();
}
}
class Outer {
private static final String NAME = "OUTER";
private final String name;
public Outer(String name) {
this.name = name;
}
static class StaticNested {
void hello() {
System.out.println("Hello" + Outer.NAME);
}
}
}