黑马程序员-笔记-泛型,黑马程序员笔记
分享于 点击 46546 次 点评:149
黑马程序员-笔记-泛型,黑马程序员笔记
-----------
[java] view plaincopy
- public class GenericTest {
- /**
- * 泛型:JDK1.6新特性。
- *
- * @throws NoSuchMethodException
- * @throws SecurityException
- * @throws InvocationTargetException
- * @throws IllegalAccessException
- * @throws InstantiationException
- * @throws IllegalArgumentException
- * @throws ClassNotFoundException
- * @throws NoSuchFieldException
- */
- public static void main(String[] args) throws SecurityException,
- NoSuchMethodException, IllegalArgumentException,
- InstantiationException, IllegalAccessException,
- InvocationTargetException, ClassNotFoundException, NoSuchFieldException {
- ArrayList list = new ArrayList();
- list.add(1);
- list.add('a');
- list.add("abc");
- // int i = (Integer) list.get(2);//类型转换错误。
- ArrayList<String> list1 = new ArrayList<String>();
- list1.add("abc");
- Constructor<String> con = String.class
- .getConstructor(StringBuffer.class);
- String str = con.newInstance(new StringBuffer("abc"));
- Class<String> c1 = String.class;
- Class<String> c2 = (Class<String>) Class.forName("java.lang.String");
- genericEqual();
- generic1();
- //genericTest(new int []{1});//泛型数组不能用基本类型代替,基本类型会单独成为一个Object
- genericTest(new Integer []{1});
- }
- public static void genericEqual() throws SecurityException,
- NoSuchMethodException, IllegalArgumentException,
- IllegalAccessException, InvocationTargetException {
- // 内存中泛型被去类型化,泛型是不会存在于字节码中的。但是它存在于一个Type类型中,Type是Class的父类。
- ArrayList<String> list1 = new ArrayList<String>();
- ArrayList<Integer> list2 = new ArrayList<Integer>();
- System.out.println(list1.getClass() == list2.getClass());
- // 通过反射,从字节码中跳过泛型限制。
- Method addMehtod = list2.getClass().getMethod("add", Object.class);
- Method getMehtod = list2.getClass().getDeclaredMethod("get", int.class);
- addMehtod.invoke(list2, "abc");
- System.out.println(list2.get(0));
- System.out.println(getMehtod.invoke(list2, 0));
- }
- public static void generic1() throws SecurityException, NoSuchMethodException, NoSuchFieldException {
- // 参数化类型与原始类型相互兼容,但注意其影响。
- List<String> list1 = new ArrayList();
- list1.add("123");
- List list2 = new ArrayList<String>();
- list2.add(123);
- // 参数化类型是不会考虑继承的。以下两种情况都错误。
- // List<String> list3 = new ArrayList<Object>();
- // List<Object> list4 = new ArrayList<String>();
- // 在创建数组实例时,数组的元素不能使用参数化类型。
- // List<String>[] lists = new List<String>[5];
- List[] lists = new List[5];
- List list5 = new ArrayList<Object>();
- list5.add(123);
- List<String> list6 = list5;
- // 虽然以上的赋值过程过了编译,但是再取出的时候,就会爆出异常。
- // !!!!!泛型的限制完全取决于左边。
- // System.out.println(list6.get(0));
- //copy(new ArrayList<String>(),new Integer[5]);//泛型的传递。
- String str = copy2("123","123");
- applyGeneric(new Vector<Date>());
- }
- public static void generic2() {
- // 占位符?的赋值是可以的。
- Collection<?> cols = new HashSet<String>();
- // cols.add("123");//错误,通配符?只能调用所有与参数无关的方法。
- System.out.println(cols.size());
- }
- // 泛型与异常。
- public static <T extends Exception> void genericException() throws T {
- try {
- } catch (Exception e) {// 此处catch出T这种类型的异常,必须要是明确的。
- throw (T) e;// 可以跑出一个不明确的异常。
- }
- }
- // 可以定义多个泛型。
- public static <X, Y, Z> void genericTest(X[] x) {
- //泛型数组不能使用基本类型。
- }
- //反省的类型传播
- //在这里的B与下边的B可以不是同一个。这些都是不会矛盾的。
- public static <B> void copy(List<B> listS,B[] a){
- }
- public static <B> B copy2(B b1,B b2){
- return b1;
- }
- //通过反射获得泛型的类型
- public static void applyGeneric(Vector<Date> v1) throws SecurityException, NoSuchMethodException, NoSuchFieldException{
- Method m = GenericTest.class.getMethod("applyGeneric", Vector.class);//通过成员方法获得。
- Type[] type = m.getGenericParameterTypes();
- ParameterizedType pType = (ParameterizedType) type[0];
- System.out.println(pType.getRawType());
- System.out.println(pType.getActualTypeArguments()[0]);
- Field v = GenericTest.class.getField("v");//通过成员变量获得。
- Type typeF =v.getGenericType();
- ParameterizedType pTypeF =(ParameterizedType)typeF;
- System.out.println(pTypeF.getRawType());
- System.out.println(pTypeF.getActualTypeArguments()[0]);
- }
- public Vector<String> v = new Vector<String>();
- }
[java] view plaincopy
- public class Play22 {
- public static void main(String[] args) {
- // 泛型的对象在明确泛型的具体类型后,就被固定了。
- Util_p22<String> up = new Util_p22<String>();
- up.setE("java");
- String s = up.getE();
- // up.setE(123);
- // 如果需要对象的不同方法,可以去操作不同的类型,那么需要在方法上声明泛型。
- up.show(213);
- up.print(true);
- List<Play22_1<String>> li = new ArrayList<Play22_1<String>>();
- Play22_1<String> litest = new Play22_1<String>();
- li.add(litest);
- up.test1(li).test();
- TreeSet<Play22_1> tset = new TreeSet<Play22_1>(new Comparator<Inter_p22>() {
- @Override
- public int compare(Inter_p22 o1, Inter_p22 o2) {
- // TODO Auto-generated method stub
- return 0;
- }
- });
- }
- }
- // 泛型定义在类上。
- class Util_p22<E> {
- private E e;
- public E getE() {
- return e;
- }
- public void setE(E e) {
- this.e = e;
- }
- // 静态方法不能访问类上定义的泛型,因为这个泛型,需要在类的实例被确定后才确定。这个方法必然不能是static。
- public <T> void show(T t) {
- System.out.println(t);
- }
- // 泛型定义在方法上,方法上单独存在的泛型是可以为static。而且可以与类上的泛型共同存在。
- // 注意书写格式。
- public static <Q> void print(Q q) {
- System.out.println(q);
- }
- // 泛型在方法上的另一种写法。?占位符,不清楚所传入的参数的类型的泛型
- // 使用?通配符,确定是,无法像正常泛型那样使用一个确定的泛型的对象。不能接受并操作。
- // 这个参数,可以是List<String>
- // List<Integer>。但是取出来的值依然是Object,不是正常泛型的String或者Integer。
- public static void test(List<?> list) {
- Iterator<?> it = list.iterator();
- while (it.hasNext()) {
- System.out.println(it.next());
- }
- }
- // 向上限定,可以接受,Inter_p22的子类型。
- public static <K extends Inter_p22> K test1(List<K> list) {
- Iterator<K> it = list.iterator();
- return it.next();
- }
- // 通配符写法
- public static void test2(List<? extends Inter_p22> list) {
- }
- //向下限定,必须是XX的父类。
- public static <K extends Play22_2> void test3(List<K> list) {
- }
- // 通配符写法
- public static void test4(List<? super Play22_2> list) {
- }
- }
- // 泛型定义在接口上。
- interface Inter_p22<T> {
- public void show(T t);
- }
- // 继承泛型接口的类。实现类,依然使用泛型。
- class Play22_1<T> implements Inter_p22<T> {
- @Override
- public void show(T t) {
- System.out.println("Play22_1");
- }
- public void test(){
- System.out.println("Play22_test");
- }
- }
- // 继承泛型接口的类。实现类,已经明确泛型。
- class Play22_2 implements Inter_p22<String> {
- @Override
- public void show(String t) {
- System.out.println("Play22_2");
- }
- }
相关文章
- 暂无相关文章
用户点评