Java 中的内部类和枚举


# Test.java

class A {
	
	class B {
		void hello() {
			System.out.println("B");
			// 实例化 A
			A a = new A();
			a.hello();
		}
	}
	
	void hello() {
		System.out.println("A");
	}
}

public class Test {
	
	public static void main(String[] args) {
		// 实例化类 B
		B b = new A().new B();
		b.hello();
	}

}

# 一个 .java 文件中有几个类就会生成几个字节码
# 该类编译后会生成三个字节码
- Test.class
- A.class
- A$B.class

# 从编译后的字节码可以看到、内部类是外部类的一个属性
# 在A类外实力化B类时、编译器会将B类作为A类的属性、而将A类传递给B类的构造器

# 接口也可以作为类的成员

class A {
	
	private interface B {
		int a = 20;
		void sleep();
	}
	
	public void hello() {
		System.out.println(B.a);
		class C implements B {
			@Override
			public void sleep() {
				System.out.println("C");
			}
		}
		C c = new C();
		c.sleep();
	}
}

public class Test {

	public static void main(String[] args) {
		A a = new A();
		a.hello();
	}

}

class A {
	int a = 20;
	
	class B {
		int a = 10;
		
		void hello() {
			System.out.println(this.a); // a = 10;
			// A.this 就是 B 类构造器中传递过来的 A类
			System.out.println(A.this.a); // a = 20;
		}
	}
	
}

public class Test {
	
	public static void main(String[] args) {
		A.B b = new A().new B();
		b.hello();
	}

}

静态内部类

# 静态内部类等价于外部类

# 静态内部类、只能访问内部类的静态成员和静态方法


class A {
	static class B {
	}
}

public class Test {
	
	public static void main(String[] args) {
		// 实例化静态内部类
		A.B b =new A.B();
	}

}

局部内部类

# 在一个方法中定义的类称为局部内部类
# 局部内部类只能在所在的方法中实例化
class A {

	void hello() {
		class B {
		}
		// 实例化局部内部类
		B b = new B();
	}

}

匿名内部类

class A {
}

public class Test {

	public static void main(String[] args) {
		A a = new A() {
			// 匿名内部类
		};
	}

}

# 编译后的字节码文件为:
- Test.class
- A.class
- Test$1.class
# 在哪个类的() 后面、分号前面、写一对花括号、这个花括号就会被编译器编译成一个继承自该类的子类
- 比如 new A() {}; 花括号中的类就被编译为A类的子类!

枚举


# 所谓枚举、就是其值可以列举完的值
# 每一个枚举都是 java.lang.Enum 的子类
# 所有枚举类型都会实现序列化和 comparable 接口

// 定义血型的枚举
enum BloodType {
	A, B, AB, O
}

interface Say {
	// 接口中的每一个字段默认都被 public static final 修饰
	// 每一个方法默认都被 public abstract 修饰
	int a = 10;
}

# 对于 bloodType 这个枚举类来说
- 里面的每一个值的类型都为 BloodType 
- 每一个值都被 public static final 修饰

public static void main(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 类型

public static void main(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
		}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值