public class Main {
public static void main(String[] args) {
Outer outer = new Outer();
outer.method1();
}
}
class Outer {
int x = 200;
void method1() {
int x = 100;
System.out.println(this);
System.out.println(Integer.toHexString(hashCode()));
class Inner {
class H{
H() {
new B();
}
class B{
B() {
new C();
}
class C{
C() {
new D();
}
class D {
D() {
System.out.println(this);
}
}
}
}
}
void f() {
System.out.println("x=" + x);
System.out.println("Outer.this.x=" + Outer.this.x);
new H();
}
}
new Inner().f();
}
}
貌似局部内部类可以一直套下去
public class Main {
public static void main(String[] args) {
// 接口的匿名类
Ixx obj = new Ixx() {
@Override
public void cry() {
System.out.println(this);
System.out.println("kid crying.....");
}
};
obj.cry();
System.out.println("obj的运行类型为:" + obj.getClass());
A a = new A() {
@Override
void method() {
super.x = 100;
System.out.println(this.x);
}
};
a.method();
System.out.println("a的运行类型为:" + a.getClass());
}
}
// anonymous 匿名的
class A {
int x;
void method() { }
}
interface Ixx {
void cry();
}
1.成员内部类定义在外部类属性的位置
2.可用(public, protected, private)修饰,因为它的地位就像一个属性
3.只能通过对象访问成员内部类
public class Main {
public static void main(String[] args) {
Outer outer = new Outer();
Outer.Inner inner = outer.new Inner(); // 创建成员内部类对象
Outer.Inner inner1 = outer.getInner(); // 直接用方法创建
System.out.println(inner.x); // 访问内部类的属性
inner.method(); // 使用内部类的方法
}
}
class Outer {
public int i;
protected int j;
int k;
class Inner {
int x = 100;
void method() {
System.out.println(x);
}
}
Inner getInner() {
return new Inner();
}
}
1.类名用static修饰
2.可以访问外部类的静态成员
3.可任意加访问修饰符,无限制
4.只能通过类名访问静态内部类
public class Main {
public static void main(String[] args) {
Outer outer = new Outer();
Outer.Inner inner = new Outer.Inner(); // 静态类直接外部类.内部类new
}
}
class Outer {
static String str = "hello";
int x = 100;
static class Inner {
static{
System.out.println(str);
System.out.println("hhhhh");
}
}
}