• Java 泛型


    结论: 暂时不指定类型, 调用再决定类型

    原则: 如果泛型方法可以取代将整个类泛型化, 那就应该只使用泛型方法

     1 package 泛型;
     2 
     3 class Teacher {    // 教师类的描述
     4 }
     5 
     6 class Student {    // 学生类的描述
     7 }
     8 
     9 //class Tool {    // 以前都这么搞
    10 //    private Object obj;
    11 //
    12 //    public Object getObject() {
    13 //        return obj;
    14 //    }
    15 //    public void setObject(Object obj) {
    16 //        this.obj = obj;
    17 //    }
    18 //}
    19 
    20 class Utils<T> { // 限定类型
    21     private T t;
    22 
    23     public T getObject() {
    24         return t;
    25     }
    26 
    27     public void setObject(T t) {
    28         this.t = t;
    29     }
    30 }
    31 
    32 public class 演变 {
    33 
    34     public static void main(String[] args) {
    35 
    36 //        Tool t = new Tool();
    37 //        t.setObject(new Teacher());
    38 //        Student s = (Student) t.getObject();    // 编译会通过
    39 
    40         /**
    41          * 1.将运行时期出现问题 ClassCastException, 转移到了编译时期
    42          * 2.避免了强制转换麻烦
    43          */
    44         Utils<Teacher> u = new Utils<Teacher>();
    45         u.setObject(new Teacher());
    46         Student s = u.getObject();                // 编译会报错
    47     }
    48 }
    package 泛型;
    
    /**
     * 泛型类定义的泛型, 在整个类中有效
     */
    //class Utils1<T> {
    //
    //    public void show(T t) {
    //        System.out.println("show:    " + t);
    //    }
    //
    //    public void print(T t) {
    //        System.out.println("print:    " + t);
    //    }
    //
    //}
    
    /**
     * 不同方法可以操作不同类型
     */
    class Utils1 {
    
        public <T> void show(T t) {
            System.out.println("show:    " + t);
        }
    
        public <T> void print(T t) {
            System.out.println("print:    " + t);
        }
    
        /**
         * 静态方法不可以访问类上定义的泛型, 可以将泛型定义在方法上。
         */
        public static <T> void method(T t) {
            System.out.println("method:    " + t);
        }
    
    }
    
    public class 高阶 {
    
        public static void main(String[] args) {
    
    //        Utils<String> u = new Utils<String>();
    //        u.show("孙旌棋");
    //        u.print(863523704);// 这样不OK, 写了直接挂
    
            Utils1 u = new Utils1();
            u.show("孑小");
            u.print(28);
        }
    }
     1 package 泛型;
     2 
     3 import java.util.*;
     4 
     5 public class 通配符 {
     6 
     7     public static void main(String[] args) {
     8 
     9         ArrayList<String> al = new ArrayList<String>();
    10 
    11         al.add("孑小");
    12         al.add("孑小");
    13         al.add("孑小");
    14         printColl(al);
    15 
    16         ArrayList<Integer> al_1 = new ArrayList<Integer>();
    17 
    18         al_1.add(28);
    19         al_1.add(28);
    20         al_1.add(28);
    21 //      printColl_1(al_1);
    22         printColl(al_1);
    23 
    24     }
    25 
    26 //    public static void printColl(ArrayList<String> al) {
    27 //        for (Iterator<String> it = al.iterator(); it.hasNext();) {
    28 //            System.out.println(it.next());
    29 //        }
    30 //    }
    31 //    public static void printColl_1(ArrayList<Integer> al) {
    32 //        for (Iterator<Integer> it = al.iterator(); it.hasNext();) {
    33 //            System.out.println(it.next());
    34 //        }
    35 //    }
    36 
    37     public static void printColl(ArrayList<?> al) {
    38         for (Iterator<?> it = al.iterator(); it.hasNext();) {
    39             System.out.println(it.next());
    40         }
    41     }
    42 }
     1 package 泛型;
     2 
     3 import java.util.*;
     4 
     5 public class 泛型上下限 {
     6     public static void main(String[] args) {
     7 
     8         ArrayList<Person> al = new ArrayList<Person>();
     9         al.add(new Person("孑小"));
    10         al.add(new Person("孑小"));
    11         al.add(new Person("孑小"));
    12         printColl(al);
    13         printColl_1(al);
    14 
    15         ArrayList<Student> al_1 = new ArrayList<Student>();
    16         al_1.add(new Student("孑小"));
    17         al_1.add(new Student("孑小"));
    18         al_1.add(new Student("孑小"));
    19         printColl(al_1);
    20         printColl_1(al_1);
    21     }
    22 
    23     // ? extends E 上限,接收E类型或E类型的子类型
    24     public static void printColl(ArrayList<? extends Person> al) {
    25         for (Iterator<? extends Person> it = al.iterator(); it.hasNext();) {
    26             System.out.println(it.next().getName());
    27         }
    28     }
    29 
    30     // ? super E 下限,接收E类型或E类型的父类型
    31     public static void printColl_1(ArrayList<? super Student> al) {
    32         for (Iterator<? super Student> it = al.iterator(); it.hasNext();) {
    33             System.out.println(((Person) it.next()).getName());
    34         }
    35     }
    36 }
  • 相关阅读:
    Diocp截图
    [DIOCP视频]-DIOCPFileServer视频
    DIOCP 运作核心探密
    DIOCP-DIOCPv5的处理能力
    【被C折腾系列】用C调DIOCP编码客户端通信
    DIOCP-V5发布
    MyBean通用报表插件介绍
    Amazon
    输出所有大小写字符的组合
    删除最少字符生成Palindrome
  • 原文地址:https://www.cnblogs.com/sunjunxi/p/8555257.html
Copyright © 2020-2023  润新知