java枚举及泛型

 

目录

 

 11.1 枚举

  11.1.1 使用枚举类型设置常量

   11.1.2 深入了解枚举类型

11.1.3 使用枚举类型的优势

11.2 泛型

11.2.1 回顾"向上转型" 与"向下转型"

11.2.2 定义泛型类

11.2.3 泛型的常规用法

11.2.4 泛型的高级用法

11.2.5 泛型总结

11.3 小结


 11.1 枚举

  11.1.1 使用枚举类型设置常量

   11.1.2 深入了解枚举类型

11.1.3 使用枚举类型的优势

11.2 泛型

11.2.1 回顾"向上转型" 与"向下转型"

11.2.2 定义泛型类

11.2.3 泛型的常规用法

11.2.4 泛型的高级用法

11.2.5 泛型总结

11.3 小结

例11.1 

public class ConstantsTest {
	enum Constants2 {//将常量放置在枚举类型中
		Constants_A ,
		Constants_B
	}

	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.doit2(Constants2.Constants_A);//使用接口中定义的常量
		ConstantsTest.doit2(Constants2.Constants_B); //使用枚举类型中的常量
		//ConstantsTest.doit2(3);
		// TODO Auto-generated method stub

	}

}

f924e2c3db194b45a7108321b5fbecc0.png

 

 例11.3


enum Constants {  //将常量放在枚举类型中
	   Constants_A,
	   Constants_B,
	   Constants_C,
	   Constants_D
}
public class EnumMethodTest { //定义比较枚举类型方法,参数类型为枚举类型
     public static void compare(Constants c) {
    	 Constants array[] = Constants.values();//根据values()方法返回的数组 //将比较结果返回
    	 for (int i =0;i < array.length;i++) {
    		 System.out.println(c + "与" + array[i] + "的比较结果为:" + c.compareTo(array[i]));
    		 
    	 }
     }  //在主方法中调用compare()方法
	public static void main(String[] args) {
		compare(Constants.valueOf("Constants_B"));
		// TODO Auto-generated method stub

	}

}

 8e2a7491b9434880b85fc1a23f6ba1e5.png

 

 例11.4


public class EnumIndexTest {
  enum Constats2 {  //将常量放在枚举类型中
	  Constants_A,
	   Constants_B,
	   Constants_C
  }
	public static void main(String[] args) {
		Constants2[] arrayCon = Constants2.values();
		for (int i =0;i<arrayCon.length;i++) {  //在循环中获取枚举类型成员的索引位置
			System.out.println(arrayCon[i] + "在枚举类型中位置索引值"
		+ arrayCon [i].ordinal());
		}
		// TODO Auto-generated method stub

	}

}

 2e8a6bbd9be2497dba0680452f3d5d1d.png

 

 例11.5

public class EnumConTest {
	enum Constants2 {  //将常量放置在枚举类型中
		 Constants_A("我是枚举成员A"), //定义带参数的枚举类型成员
		   Constants_B("我是枚举成员B"),
		   Constants_C("我是枚举成员C"),
		   Constants_D(3);
		private String description;
		private int i =4;
		//定义参数为String型的构造方法
		private Constants2(String description) {
			this.description = description;
		}
	 private Constants2(int i) { //定义参数为int型的构造方法
		 this.i = this.i + i;
	 }
	 public String getDescription() { //获取description的值
		 return description;
	 }
	 public int getI() { //获取i的值
		 return i ;
	 }
	}
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Constants2 array[] = Constants2.values(); //获取枚举成员数组
		for(int i = 0;i<array.length;i++) {
			System.out.println(array[i] + "调用getDescription()方法为:" +array[i]. getDescription());
		}
    Constants2 c2 = Constants2.valueOf("Constants_D"); //将字符串转换成枚举对象
    System.out.println(c2 + "调用getI()方法为:" + c2.getI());
	}

}

ef6b8889f66c410f857a23b647ca619f.png

 

例11.7

public class Test {
private Object b;   //定义Object类型成员变量
public Object getB() {  //设置相应的getxxx()方法
	return b;
}
		public void setB(Object b) {  //设置相应的setxxx()方法
			this.b = b;
		}
		public static void main(String[] args) {
			// TODO Auto-generated method stub
			Test t = new Test();
			t.setB(new Boolean(true));  //向上转型操作
			System.out.println(t.getB());
			t.setB(new Float(12.3));
			Float f = (Float) (t.getB()); //向下转型操作
			System.out.println(f);
	}

}

2ee2cea38f6c4069bc069a61c886a9a5.png

 

1. 通过类型参数T的继承限制泛型类型

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

public class LimitClass <T extends List> {  //限制泛型的类型

	public static void main(String[] args) {  //可以实例化已经实现的list接口的类
		// TODO Auto-generated method stub
		LimitClass<ArrayList> l1 = new LimitClass<ArrayList>();
		LimitClass<LinkedList> l2 = new LimitClass<LinkedList>();
		//这句是错误的,因为HashMap没有实现List()接口
		//LimitListClass<HashMap> l3 = new LimitClass<HashMap>();
		

	}

}

例11.11

import java.util.*;

public class WildClass {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		List<String> l1 = new ArrayList<String>();  //创建一个ArrayList对象
l1.add("成员"); //在集合中添加内容
List<?> l2 = l1;  //使用通配符
List<?> l3 = new LinkedList<Integer>();
System.out.println("l1:" +l1.get(0)); //获取l1集合中第一个值
System.out.println("l2:" +l2.get(0)); //获取l2集合中第一个值
l1.set(0, "成员改变"); //使用通配符的对象不能调用set()方法
//l2.add("添加");    //使用通配符的对象不能调用add方法
// l2.set(0,"成员改变"); //使用通配符的对象不能调用set方法
//13.add(1);
//13.set(0,1);
System.out.println("l1:" +l1.get(0));
	}

}

012bd8997f694b5f889088462a73e5c8.png

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值