内部类可以被继承,不会被覆盖。
内部类主要了解明白,具体暂时不深究
/**
* 一个简单的内部类
*/
public class Outer {
class Inner {
private String name;
public Inner(String name) {
super();
this.name = name;
}
@Override
public String toString() {
return "Inner [name=" + name + "]";
}
}
public Inner findInner(String name) {
return new Inner(name);
}
public static void main(String[] args) {
// 通过外部类方法获取内部类
Outer outer = new Outer();
Inner inner = outer.findInner("inner");
System.out.println(inner.toString());
// 直接new内部类
Inner inner2 = new Outer().new Inner("inner2");
System.out.println(inner2.toString());
// 匿名内部类
new Thread(new Runnable() {
@Override
public void run() {
System.out.println("hello world");
}
}).start();
}
}