枚举(enum)
ava 枚举是一个特殊的类,一般表示一组常量,比如一年的 4 个季节,一个年的 12 个月份,一个星期的 7 天,方向有东南西北等。
Java 枚举类使用 enum 关键字来定义,各个常量使用逗号 , 来分割。
例如定义一个颜色的枚举类。
1 enum Color 2 { 3 RED, GREEN, BLUE; 4 } 5 6 public class Test 7 { 8 // 执行输出结果 9 public static void main(String[] args) 10 { 11 Color c1 = Color.RED; 12 System.out.println(c1); 13 } 14 }
执行以上代码输出结果为:
RED
内部类中使用枚举
public class Test { enum Color { RED, GREEN, BLUE; } // 执行输出结果 public static void main(String[] args) { Color c1 = Color.RED; System.out.println(c1); } }
结果也是RED
每个枚举都是通过 Class 在内部实现的,且所有的枚举值都是 public static final 的。
以上的枚举类 Color 转化在内部类实现:
class Color { public static final Color RED = new Color(); public static final Color BLUE = new Color(); public static final Color GREEN = new Color(); }
迭代枚举元素
1 enum Color 2 { 3 RED, GREEN, BLUE; 4 } 5 public class MyClass { 6 public static void main(String[] args) { 7 for (Color myVar : Color.values()) { 8 System.out.println(myVar); 9 } 10 } 11 }
执行以上代码输出结果为:
RED GREEN BLUE
在 switch 中使用枚举类
enum Color { RED, GREEN, BLUE; } public class MyClass { public static void main(String[] args) { Color myVar = Color.BLUE; switch(myVar) { case RED: System.out.println("红色"); break; case GREEN: System.out.println("绿色"); break; case BLUE: System.out.println("蓝色"); break; } } }
结果为:蓝色
values(), ordinal() 和 valueOf() 方法
enum 定义的枚举类默认继承了 java.lang.Enum 类,并实现了 java.lang.Serializable 和 java.lang.Comparable 两个接口。
values(), ordinal() 和 valueOf() 方法位于 java.lang.Enum 类中:
- values() 返回枚举类中所有的值。
- ordinal()方法可以找到每个枚举常量的索引,就像数组索引一样。
- valueOf()方法返回指定字符串值的枚举常量。
enum Color { RED, GREEN, BLUE; } public class Test { public static void main(String[] args) { // 调用 values() Color[] arr = Color.values(); // 迭代枚举 for (Color col : arr) { // 查看索引 System.out.println(col + " at index " + col.ordinal()); } // 使用 valueOf() 返回枚举常量,不存在的会报错 IllegalArgumentException System.out.println(Color.valueOf("RED")); // System.out.println(Color.valueOf("WHITE")); } } 执行以上代码输出结果为: RED at index 0 GREEN at index 1 BLUE at index 2 RED
枚举类成员
枚举跟普通类一样可以用自己的变量、方法和构造函数,构造函数只能使用 private 访问修饰符,所以外部无法调用。
枚举既可以包含具体方法,也可以包含抽象方法。 如果枚举类具有抽象方法,则枚举类的每个实例都必须实现它。
enum Color { RED, GREEN, BLUE; // 构造函数 private Color() { System.out.println("Constructor called for : " + this.toString()); } public void colorInfo() { System.out.println("Universal Color"); } } public class Test { // 输出 public static void main(String[] args) { Color c1 = Color.RED; System.out.println(c1); c1.colorInfo(); } } 执行以上代码输出结果为: Constructor called for : RED Constructor called for : GREEN Constructor called for : BLUE RED Universal Color
枚举enum实现抽象方法
枚举类中的抽象方法实现,需要枚举类中的每个对象都对其进行实现。
enum Color{ RED{ public String getColor(){//枚举对象实现抽象方法 return "红色"; } }, GREEN{ public String getColor(){//枚举对象实现抽象方法 return "绿色"; } }, BLUE{ public String getColor(){//枚举对象实现抽象方法 return "蓝色"; } }; public abstract String getColor();//定义抽象方法 } public class Test{ public static void main(String[] args) { for (Color c:Color.values()){ System.out.print(c.getColor() + "、"); } } }
Java 泛型
Java 泛型(generics)是 JDK 5 中引入的一个新特性, 泛型提供了编译时类型安全检测机制,该机制允许程序员在编译时检测到非法的类型。
泛型的本质是参数化类型,也就是说所操作的数据类型被指定为一个参数。
假定我们有这样一个需求:写一个排序方法,能够对整型数组、字符串数组甚至其他任何类型的数组进行排序,该如何实现?
答案是可以使用 Java 泛型。
使用 Java 泛型的概念,我们可以写一个泛型方法来对一个对象数组排序。然后,调用该泛型方法来对整型数组、浮点数数组、字符串数组等进行排序。
java 中泛型标记符:
- E - Element (在集合中使用,因为集合中存放的是元素)
- T - Type(Java 类)
- K - Key(键)
- V - Value(值)
- N - Number(数值类型)
- ? - 表示不确定的 java 类型
实例
下面的例子演示了如何使用泛型方法打印不同类型的数组元素:
public class GenericMethodTest { // 泛型方法 printArray public static < E > void printArray( E[] inputArray ) { // 输出数组元素 for ( E element : inputArray ){
//%s就是后面类型为字符窜 System.out.printf( "%s ", element ); } System.out.println(); } public static void main( String args[] ) { // 创建不同类型数组: Integer, Double 和 Character Integer[] intArray = { 1, 2, 3, 4, 5 }; Double[] doubleArray = { 1.1, 2.2, 3.3, 4.4 }; Character[] charArray = { 'H', 'E', 'L', 'L', 'O' }; System.out.println( "整型数组元素为:" ); printArray( intArray ); // 传递一个整型数组 System.out.println( "\n双精度型数组元素为:" ); printArray( doubleArray ); // 传递一个双精度型数组 System.out.println( "\n字符型数组元素为:" ); printArray( charArray ); // 传递一个字符型数组 } }
编译以上代码,运行结果如下所示:
整型数组元素为:
1 2 3 4 5
双精度型数组元素为:
1.1 2.2 3.3 4.4
字符型数组元素为:
H E L L O
1 public class MaximumTest 2 { 3 // 比较三个值并返回最大值 4 public static <T extends Comparable<T>> T maximum(T x, T y, T z) 5 { 6 T max = x; // 假设x是初始最大值 7 if ( y.compareTo( max ) > 0 ){ 8 max = y; //y 更大 9 } 10 if ( z.compareTo( max ) > 0 ){ 11 max = z; // 现在 z 更大 12 } 13 return max; // 返回最大对象 14 } 15 public static void main( String args[] ) 16 { 17 System.out.printf( "%d, %d 和 %d 中最大的数为 %d\n\n", 18 3, 4, 5, maximum( 3, 4, 5 ) ); 19 20 System.out.printf( "%.1f, %.1f 和 %.1f 中最大的数为 %.1f\n\n", 21 6.6, 8.8, 7.7, maximum( 6.6, 8.8, 7.7 ) ); 22 23 System.out.printf( "%s, %s 和 %s 中最大的数为 %s\n","pear", 24 "apple", "orange", maximum( "pear", "apple", "orange" ) ); 25 } 26 }
编译以上代码,运行结果如下所示:
3, 4 和 5 中最大的数为 5 6.6, 8.8 和 7.7 中最大的数为 8.8 pear, apple 和 orange 中最大的数为 pear
泛型类
泛型类的声明和非泛型类的声明类似,除了在类名后面添加了类型参数声明部分。
和泛型方法一样,泛型类的类型参数声明部分也包含一个或多个类型参数,参数间用逗号隔开。一个泛型参数,也被称为一个类型变量,是用于指定一个泛型类型名称的标识符。因为他们接受一个或多个参数,这些类被称为参数化的类或参数化的类型。
实例
public class Box<T> { private T t; public void add(T t) { this.t = t; } public T get() { return t; } public static void main(String[] args) { Box<Integer> integerBox = new Box<Integer>(); Box<String> stringBox = new Box<String>(); integerBox.add(new Integer(10)); stringBox.add(new String("菜鸡")); System.out.printf("整型值为 :%d\n\n", integerBox.get()); System.out.printf("字符串为 :%s\n", stringBox.get()); } }
编译以上代码,运行结果如下所示:
整型值为 :10
字符串为 :菜鸡
类型通配符
1、类型通配符一般是使用 ? 代替具体的类型参数。例如 List<?> 在逻辑上是 List<String>,List<Integer> 等所有 List<具体类型实参> 的父类。
import java.util.*; public class GenericTest { public static void main(String[] args) { List<String> name = new ArrayList<String>(); List<Integer> age = new ArrayList<Integer>(); List<Number> number = new ArrayList<Number>(); name.add("icon"); age.add(18); number.add(314); getData(name); getData(age); getData(number); } public static void getData(List<?> data) { System.out.println("data :" + data.get(0)); } }
输出结果为:
data :icon
data :18
data :314
解析: 因为 getData() 方法的参数是 List<?> 类型的,所以 name,age,number 都可以作为这个方法的实参,这就是通配符的作用。
2、类型通配符上限通过形如List来定义,如此定义就是通配符泛型值接受Number及其下层子类类型。
1 import java.util.*; 2 3 public class GenericTest { 4 5 public static void main(String[] args) { 6 List<String> name = new ArrayList<String>(); 7 List<Integer> age = new ArrayList<Integer>(); 8 List<Number> number = new ArrayList<Number>(); 9 10 name.add("icon"); 11 age.add(18); 12 number.add(314); 13 14 //getUperNumber(name);//1 15 getUperNumber(age);//2 16 getUperNumber(number);//3 17 18 } 19 20 public static void getData(List<?> data) { 21 System.out.println("data :" + data.get(0)); 22 } 23 24 public static void getUperNumber(List<? extends Number> data) { 25 System.out.println("data :" + data.get(0)); 26 } 27 } 28 29 输出结果: 30 31 data :18 32 data :314
解析: 在 //1 处会出现错误,因为 getUperNumber() 方法中的参数已经限定了参数泛型上限为 Number,所以泛型为 String 是不在这个范围之内,所以会报错。
3、类型通配符下限通过形如 List<? super Number> 来定义,表示类型只能接受 Number 及其上层父类类型,如 Object 类型的实例。