classA{classB{voidhello(){
System.out.println("B");// 实例化 A
A a =newA();
a.hello();}}voidhello(){
System.out.println("A");}}publicclassTest{publicstaticvoidmain(String[] args){// 实例化类 B
B b =newA().newB();
b.hello();}}
classA{privateinterfaceB{int a =20;voidsleep();}publicvoidhello(){
System.out.println(B.a);classCimplementsB{@Overridepublicvoidsleep(){
System.out.println("C");}}
C c =newC();
c.sleep();}}publicclassTest{publicstaticvoidmain(String[] args){
A a =newA();
a.hello();}}
classA{int a =20;classB{int a =10;voidhello(){
System.out.println(this.a);// a = 10;// A.this 就是 B 类构造器中传递过来的 A类
System.out.println(A.this.a);// a = 20;}}}publicclassTest{publicstaticvoidmain(String[] args){
A.B b =newA().newB();
b.hello();}}
静态内部类
# 静态内部类等价于外部类
# 静态内部类、只能访问内部类的静态成员和静态方法
classA{staticclassB{}}publicclassTest{publicstaticvoidmain(String[] args){// 实例化静态内部类
A.B b =newA.B();}}
局部内部类
# 在一个方法中定义的类称为局部内部类
# 局部内部类只能在所在的方法中实例化
classA{voidhello(){classB{}// 实例化局部内部类
B b =newB();}}
匿名内部类
classA{}publicclassTest{publicstaticvoidmain(String[] args){
A a =newA(){// 匿名内部类};}}
// 定义血型的枚举enum BloodType {
A, B, AB, O
}interfaceSay{// 接口中的每一个字段默认都被 public static final 修饰// 每一个方法默认都被 public abstract 修饰int a =10;}
# 对于 bloodType 这个枚举类来说
- 里面的每一个值的类型都为 BloodType
- 每一个值都被 public static final 修饰
publicstaticvoidmain(String[] args){
BloodType a = BloodType.A;
System.out.println(a);// A
System.out.println(a.equals("A"));// false
System.out.println(a.getClass());// class cn.liuweiwei.innerclass.BloodType}
- 针对于我们定义的每一个枚举类型、编译器都会自动为枚举类型加入方法
- ordinal() 获取枚举值的序号
- toString() 直接输出枚举对象的内容
- values() **静态方法** 返回一个数组、该数组为所有的枚举值
- valueOf() 把一个字符串、解析为该枚举类型
# 什么是解析?什么是格式化?
- 解析就是把字符串解析为其他类型
- 格式化就是把其他类型格式化为字符串
# Switch 中可以应用的类型为 int short byte Byte Integer Short Long long 和枚举(enum)
- 从 jdk 1.7 开始 switch 支持 String 类型
publicstaticvoidmain(String[] args){
System.out.println(BloodType.A.ordinal());// 0
BloodType[] types = BloodType.values();for(BloodType bloodType : types){
System.out.print(bloodType);// A B AB O}}