枚举类型和泛型

------Java培训、Android培训、iOS培训、.Net培训、期待与您交流! -------

一.枚举类型

JDK1.5中新增了枚举类型,可以取代以往定义常量的方式,同时枚举类型还赋予程序在编译时检查的功能。

<span style="font-family:SimSun;font-size:18px;">interface Constants { // 将常量放置在接口中
	public static final int Constants_A = 1;
	public static final int Constants_B = 12;
}

public class ConstantsTest {
	enum Constants2 { // 将常量放置在枚举类型中
		Constants_A, Constants_B
	}
	
	// 使用接口定义常量
	public static void doit(int c) { // 定义一个方法,这里的参数为int型
		switch (c) { // 根据常量的值做不同操作
			case Constants.Constants_A:
				System.out.println("doit() Constants_A");
				break;
			case Constants.Constants_B:
				System.out.println("doit() Constants_B");
				break;
		}
	}
	// 定义一个方法,这里的参数为枚举类型对象
	public static void doit2(Constants2 c) { 
		switch (c) { // 根据枚举类型对象做不同操作
			case Constants_A:
				System.out.println("doit2() Constants_A");
				break;
			case Constants_B:
				System.out.println("doit2() Constants_B");
				break;
		}
	}
	
	public static void main(String[] args) {
		ConstantsTest.doit(Constants.Constants_A); // 使用接口中定义的常量
		ConstantsTest.doit2(Constants2.Constants_A); // 使用枚举类型中的常量
		ConstantsTest.doit2(Constants2.Constants_B); // 使用枚举类型中的常量
		ConstantsTest.doit(3);
		// ConstantsTest.doit2(3);
	}
}
</span>

常用方法:values(),valueof(),compareto(),ordinal(),

<span style="font-family:SimSun;font-size:18px;">import static java.lang.System.*;


public class EnumIndexTest {
	enum Constants2 { // 将常量放置在枚举类型中
		Constants_A, Constants_B, Constants_C
	}
	
	public static void main(String[] args) {
		for (int i = 0; i < Constants2.values().length; i++) {
			// 将枚举成员变量打印
			out.println("枚举类型成员变量:" + Constants2.values()[i]);
		}
		compare(Constants2.valueOf("Constants_B"));
		
		for (int i = 0; i < Constants2.values().length; i++) {
			// 在循环中获取枚举类型成员的索引位置
			out.println(Constants2.values()[i] + "在枚举类型中位置索引值"
					+ Constants2.values()[i].ordinal());
		}
	}
	// 定义比较枚举类型方法,参数类型为枚举类型
	public static void compare(Constants2 c) {
		// 根据values()方法返回的数组做循环操作
		for (int i = 0; i < Constants2.values().length; i++) {
			// 将比较结果返回
			out.println(c + "与" + Constants2.values()[i] + "的比较结果为:"
					+ c.compareTo(Constants2.values()[i]));
		}
	}
	
}</span>

构造方法:在枚举类型中,可以添加构造方法,但是必须用private修饰。

<span style="font-family:SimSun;font-size:18px;">import static java.lang.System.*;

public class EnumIndexTest {
	enum Constants2 { // 将常量放置在枚举类型中
		Constants_A("我是枚举成员A"), // 定义带参数的枚举类型成员
		Constants_B("我是枚举成员B"), Constants_C("我是枚举成员C"), Constants_D(3);
		private String description;
		private int i = 4;
		
		private Constants2() {
		}
		 // 定义参数为String型的构造方法
		private Constants2(String description) {
			this.description = description;
		}
		
		private Constants2(int i) { // 定义参数为整型的构造方法
			this.i = this.i + i;
		}
		
		public String getDescription() { // 获取description的值
			return description;
		}
		
		public int getI() { // 获取i的值
			return i;
		}
	}
	
	public static void main(String[] args) {
		for (int i = 0; i < Constants2.values().length; i++) {
			out.println(Constants2.values()[i] + "调用getDescription()方法为:"
					+ Constants2.values()[i].getDescription());
		}
		out.println(Constants2.valueOf("Constants_D") + "调用getI()方法为:"
				+ Constants2.valueOf("Constants_D").getI());
	}
}</span>

使用枚举类型的优势:类型安全,紧凑有效的数据定义,可以和程序其他部分完美交互,运行效率高。

二.泛型

在JDK1.5中定义了泛型概念,泛型实质上就是使程序员定义安全的类型。

泛型的常规用法:

  1. 定义泛型类是声明多个类型
  2. 定义方兴雷是声明数组类型
  3. 集合类声明容器的元素:可以使用K,V两个字符代表容器中的键值和键值相对应的具体值
泛型的高级用法:
  1. 限制泛型可用类型:class 类名称<T extends anyClass>,使用限制后,泛型类的类型必须实现或继承了anyClass这个借口或者类。
  2. 使用类型通配符:泛型类名称<? extends List> a=null;作用是在创建一个泛型类对象时限制这个泛型类的类型实现或继承某个借口或类的子类。
  3. 继承泛型类与实现泛型借口:定义为单行的类也可以被继承和实现。
泛型的使用方法总结:
  1. 泛型的类型参数只能是类类型,不可以是简单类型,如A<int>这种泛型定义就是错误的
  2. 泛型的类型参数可以是多个。
  3. 可以使用extends关键字限制泛型的类型
  4. 可以使用通配符限制泛型的类型。
<span style="font-family:SimSun;font-size:18px;">import java.util.*;

public class UseCase3<T> {
	public void doSomething(UseCase3<? extends List<Object>> a) {
		System.out.println(a.getClass().getName());
	}
	
	public static void main(String[] args) {
		// TODO 自动生成方法存根
		UseCase3<? extends List<Object>> a = new UseCase3<ArrayList<Object>>();
		a.doSomething(new UseCase3<ArrayList<Object>>());
		a.doSomething(new UseCase3<LinkedList<Object>>());
		UseCase3<? super List<Object>> a2 = null;
		a2 = new UseCase3<Object>();
	}
}</span>



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值