枚举类型和泛型
枚举类型
使用枚举类型可以取代以往定义常量的方式,同时枚举类型还赋予程序在编译时进行检查的功能。
在项目中创建Constans接口,在该接口中定义两个整形变量,其修饰符都是static和final;之后定义名称为Constans2的枚举类,将Constants接口的常量放置在该枚举中;最后,创建名称为Constans的类文件,在该类中通过doit()和doit2()进行不同方式的调用,然后再通过主方法进行调用,体现枚举类型定义常量的方式。
package com.sbx.myenum; /** * Created by BlueLover * Name:苏半夏 * Date:2021/2/25 * Time:20:05 */ 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){ switch (c){ case Constants.Constants_A: System.out.println("doit() Constans_A"); break; case Constants.Constants_B: System.out.println("doit() Constans_B"); break; } } public static void doit2(Constants2 c){ switch (c){ case Constants_A: System.out.println("doit2() Constans_A"); break; case Constants_B: System.out.println("doit2() Constans_B"); break; } } public static void main(String[] args) { ConstantsTest.doit(Constants.Constants_A); //使用接口中定义的常量 ConstantsTest.doit2(Constants2.Constants_A); //使用枚举类型中的常量 ConstantsTest.doit2(Constants2.Constants_A); //使用枚举类型中的常量 ConstantsTest.doit(3); //不使用枚举或接口的常量 } }
枚举常用方法练习:
package com.sbx.myenum; /** * Created by BlueLover * Name:苏半夏 * Date:2021/2/25 * Time:20:15 */ public class Test { /** * 在枚举类型中,可以添加构造方法,但是必须用private修饰 * 使用枚举的优势: * 1. 类型安全 * 2. 紧凑有效的数据定义 * 3. 可以和程序其他部分完美交互 * 4. 运行效率高 */ interface d{ public String getDescription(); } enum Constants2 implements d{ //将常量放在枚举类型中 Constants_A, Constants_B, Constants_C("我是枚举成员C"), Constants_D(3), Constants_E{ //可以在枚举成员内部继承接口 public String getDescription() { return ("我是枚举成员E"); } }; private String description; private int i = 10; //定义默认构造方法 Constants2(){ } //定义带参数的构造方法,参数类型为字符串型 Constants2(String description) { this.description = description; } //定义带参数的构造方法,参数类型为整型 Constants2(int i) { this.i = this.i+i; } public int getI() { return i; } //也可以在外部继承接口 public String getDescription() { return this.description = description; // return "外部"; } } //定义比较枚举类型方法,参数类型为枚举类型 public static void compare(Constants2 c){ for (int i = 0; i < Constants2.values().length; i++) { /** * 2.compareTo(enum2) 方法用于比较两个枚举对象在定义时的顺序 ,返回值为差值,即 调用者-enum2 */ //将比较结果返回 System.out.println(c+"与"+Constants2.values()[i]+"的比较结果为:"+c.compareTo(Constants2.values()[i])); } } /** 1.values() 将枚举类型成员以数组的形式返回 */ public static void main(String[] args) { for (int i = 0; i < Constants2.values().length; i++) { //将枚举成员变量打印 System.out.println("枚举类型成员变量"+Constants2.values()[i]); /** 3.ordinal()方法用于获取某个枚举对象的位置索引值 */ System.out.println(Constants2.values()[i]+"在枚举类型中索引的位置"+Constants2.values()[i].ordinal()); } System.out.println("==========================="); //调用Compare()方法 compare(Constants2.valueOf("Constants_B")); //调用getDescription方法 System.out.println(Constants2.values()[2].getDescription()); System.out.println(Constants2.values()[3].getDescription()); //没有返回null System.out.println(Constants2.values()[4].getDescription()); //调用的是枚举成员内部的方法 //调用getI()方法 System.out.println(Constants2.values()[2].getI()); //没有值默认为0 System.out.println(Constants2.values()[3].getI()); } }
泛型
泛型的出现就是为了定义安全的类型,在此之前,常用的是向上转型和向下转型
向上转型就是将对象强制转换成Object类或者父类
向下转型就是将对象强制转换成子类
泛型的语法:
类名<T> T代表一个类型的名称
在定义泛型时,一般类型名称使用 T 来表达,而容器的元素使用 E 来表达
package com.sbx.generics; /** * Created by BlueLover * Name:苏半夏 * Date:2021/2/25 * Time:21:16 */ public class Test<T> { //定义泛型类 private T over; //定义泛型成员变量 public T getOver() { return over; } public void setOver(T over) { this.over = over; } public static void main(String[] args) { //实例化一个Boolean型的对象 Test<Boolean> over1 = new Test<Boolean>(); //实例化一个Float型的对象 Test<Float> over2 = new Test<Float>(); over1.setOver(true); //不需要进行类型转换 over2.setOver(12.3f); //不需要进行类型转换 } }
泛型的常规用法:
1.定义泛型类时声明多个类型
例如:MutiOverClass<Boolean,Float> = new MutiOverClass<Boolean,Float>();
2.定义泛型类时声明数组类型
注意点:使用泛型时,是声明一个数组,而不是创建一个数组的实例,换句话说 private T[] array = new T[10];这是错误的
package com.sbx.generics; import java.util.Map; /** * Created by BlueLover * Name:苏半夏 * Date:2021/2/25 * Time:21:29 */ public class Test2<T>{ private T[] array; //定义泛型数组 public void setArray(T[] array) { this.array = array; } public T[] getArray(){ //获取成员数组 return array; } public static void main(String[] args) { Test2<String> a = new Test2<String >(); String[] array = {"成员1","成员2","成员3","成员4","成员5"}; a.setArray(array); for (int i = 0; i < a.getArray().length; i++) { System.out.println(a.getArray()[i]); } } }
3.集合类声明容器的元素
可以使用 K 和 V 两个字符代表容器中的键值和与键值相对应的具体值。
例如:Map<Integer,String> test3 = new HashMap<Integer,String>();
常用的被泛型化的集合类
ArrayList ArrayList<E> ArrayList<Integer> test3 = new ArrayList<Integer>();
HashMap HashMap<K ,V > Map<Integer,String> test3 = new HashMap<Integer,String>();
HashSet HashSet<E> Set<Integer> test3 = new HashSet<Integer>();
Vector Vector<E> Vector<Integer> test3 = new Vector<Integer>();
package com.sbx.generics; import java.util.HashMap; import java.util.Map; /** * Created by BlueLover * Name:苏半夏 * Date:2021/2/25 * Time:21:44 */ public class Test3<K,V> { public static void main(String[] args) { Map<Integer,String> test3 = new HashMap<Integer,String>(); //定义一个集合HashMap实例 for (int i = 0; i < 5; i++) { //根据集合的长度循环将键名与具体值放入集合中 test3.put(i,"我是集合成员"+i); } for (int i = 0; i < test3.size(); i++) { //调用get()方法获取集合中的键 System.out.println(test3.get(i)); } } }
泛型的高级用法
1.限制泛型可用类型
语法:class 类名称 <T extends anyClass> 泛型类的类型必须实现或继承了anyClass这个接口或类
注意点:extends是向下限制,当然也可以向上限制,比如使用super关键字
2.使用类型通配符
语法:泛型类名称 < ? extends List> a = null; 创建一个泛型类对象时限制这个泛型类的类型实现或继承某个接口或类的子类。
3.继承泛型类与实现泛型接口
//1.限制泛型可用类型 static class LimitClass<T extends List>{ public static void main(String[] args) { //可以实例化已经实现List接口的类,例如:ArrayList和LinkedList LimitClass<ArrayList> l1 = new LimitClass<ArrayList>(); } } public static void main(String[] args) { //2.使用类型通配符 A <? extends List> a = null; a = new A<ArrayList>(); // a = new A<HashMap>(); 编译器报错,因为HashMap没有实现List接口 }
那天我还是偷偷的去见了你,回来后,果不其然的若有所失

浙公网安备 33010602011771号