1 //定义泛型类(没有构造方法,标识符无限制 2 class clazz<T>{ 3 private T x; 4 public T get() { 5 return x; 6 } 7 public void set(T s) { 8 this.x=s; 9 } 10 public void secend_main() { 11 clazz<Integer> a1=new clazz<Integer>();//实例化Integer型对象 12 a1.set(11);//set不需要进行显式向上转型,直接接受相应类型的参数 13 int x=a1.get(); 14 System.out.println("泛型的引用"+x); 15 } 16 } 17 public class work_meiju { 18 //使用枚举类型定义常量 19 public enum Constants{ 20 constant_a(), 21 constant_b, 22 constant_c; 23 private String x; 24 private Constants() { 25 //定义无参构造方法(缺少会报错) 26 } 27 private Constants(String x){ 28 this.x=x; 29 } 30 private String get(){ 31 return x; 32 } 33 } 34 //以枚举类型做参量 35 public static void doit(Constants a) { 36 System.out.println(a); 37 } 38 public static void main(String[] args) { 39 //枚举类型变量的使用 40 new work_meiju().doit(Constants.constant_a); 41 //valueOf 42 //Constants.constant_a=Constants.valueOf("a"); 43 //values() 44 for(int i=0;i<Constants.values().length;i++) { 45 System.out.println(Constants.values()[i]); 46 } 47 //compareTo() 48 System.out.println("a与a的位置比较为"+Constants.constant_a.compareTo(Constants.constant_a)); 49 //ordinal() 50 System.out.println("索引位置"+Constants.constant_a.ordinal()); 51 //调用枚举类方法 52 System.out.println(Constants.values()[0].get( )); //问题:一定要用values才可以调用吗? 53 /* 54 * 55 * 泛型 56 */ 57 clazz m=new clazz(); 58 m.secend_main(); 59 } 60 61 }
打卡-枚举类型基础(一)
猜你喜欢
转载自www.cnblogs.com/TuTu-winer/p/10853034.html
今日推荐
周排行