------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中定义了泛型概念,泛型实质上就是使程序员定义安全的类型。
泛型的常规用法:
- 定义泛型类是声明多个类型
- 定义方兴雷是声明数组类型
- 集合类声明容器的元素:可以使用K,V两个字符代表容器中的键值和键值相对应的具体值
泛型的高级用法:
- 限制泛型可用类型:class 类名称<T extends anyClass>,使用限制后,泛型类的类型必须实现或继承了anyClass这个借口或者类。
- 使用类型通配符:泛型类名称<? extends List> a=null;作用是在创建一个泛型类对象时限制这个泛型类的类型实现或继承某个借口或类的子类。
- 继承泛型类与实现泛型借口:定义为单行的类也可以被继承和实现。
泛型的使用方法总结:
- 泛型的类型参数只能是类类型,不可以是简单类型,如A<int>这种泛型定义就是错误的
- 泛型的类型参数可以是多个。
- 可以使用extends关键字限制泛型的类型
- 可以使用通配符限制泛型的类型。
<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>