zoukankan      html  css  js  c++  java
  • java_泛型

    此为毕向东老师的上课笔记,搬运到此只为方便自己查看

    泛型:jdk1.5以后出现的安全机制

    好处:

      1.将运行时的问题ClassCastException转到了编译时期

      2.避免了强转的麻烦

    <>:什么时候用?

    答:当操作的引用数据类型不确定的时候就用<>,将要操作的引用数据类型传入即可,其实<>就是一个用于接收具体引用数据类型的参数范围

    在程序中只要用到了带有<>的类或者接口,就要明确传入的具体引用数据类型

    泛型技术是给编译器使用的技术,用于编译时期,确保了类型的安全,运行时,会将泛型去掉,生成的class文件中是不带泛型的,这个称为泛型的擦除

    泛型在集合中的应用:

    Person类是已定义类

    import java.util.Iterator;
    import java.util.TreeSet;
    
    public class GenericDemo {
        public static void main(String[] args) {
            TreeSet<Person> ts = new TreeSet<Person>();
            ts.add(new Person("lisi",12));
            ts.add(new Person("lisi2",13));
            ts.add(new Person("lisi3",14));
            ts.add(new Person("lisi4",15));
            Iterator<Person> it = ts.iterator();
            while(it.hasNext()) {
                Person p = it.next();
                System.out.println(p);
            }
        }
    }

    运行结果:Exception in thread "main" java.lang.ClassCastException: collectiondemo.Person cannot be cast to java.lang.Comparable

    解决:使Person类继承Comparable接口,重写compareTo(Person p)方法,

    public int compareTo(Person p){
              int temp = this.age - p.age;
              return temp ==0?this.name.compareTo(p.name):temp;
    }

    泛型类:

    在jdk.15以后,使用泛型来接收类中要操作的引用数据类型

    泛型类什么时候使用?答:当类中的操作引用数据类型不确定的时候,就使用泛型来表示

    例:父类Person类,子类有Student类和worker类

    工具类
    public class Tool<QQ> {
        private QQ q;
    
        public QQ getQ() {
            return q;
        }
    
        public void setQ(QQ q) {
            this.q = q;
        }
        
    }
    主函数:
    public class GenericDemo2 {
        public static void main(String[] args) {
            Tool<Student> tool = new Tool<Student>();
            tool.setQ(new Student());
            Student stu = tool.getQ();
        }
    }

    泛型定义在接口上:

    package cn.itcast.p4.generic.define.demo;
    
    public class GenericDefineDemo5 {
    
        /**
         * @param args
         */
        public static void main(String[] args) {
    
            InterImpl in = new InterImpl();
            in.show("abc");
            
            InterImpl2<Integer> in2 = new InterImpl2<Integer>();
            in2.show(5);
        }
    }
    
    //泛型接口,将泛型定义在接口上。 
    interface Inter<T>{
        public void show(T t);
    }
    
    
    class InterImpl2<Q> implements Inter<Q>{
        public void show(Q q){
            System.out.println("show :"+q);
        }
    }
    
    
    
    
    class InterImpl implements Inter<String>{
        public void show(String str){
            System.out.println("show :"+str);
        }
    }

    泛型定义在方法上:

        /**
         * 将泛型定义在方法上。
         * @param str
         */
        public <W> void show(W str){
            System.out.println("show : "+str.toString());
        }
        public void print(QQ str){
            System.out.println("print : "+str);
        }
        
        /**
         * 当方法静态时,不能访问类上定义的泛型。如果静态方法使用泛型,
         * 只能将泛型定义在方法上。 
         * @param obj
         */
        public static <Y> void method(Y obj){
            System.out.println("method:"+obj);

    打发打发点

  • 相关阅读:
    完整的 mime type 列表
    安装chrome扩展json-handle
    在没有go-pear.bat的php中安装pear
    windows下安装PhpDocumentor(phpdoc)笔记
    【MySQL】批量数据循环插入
    thinkPHP 3.2.3操作MongoDB指南
    给深度学习入门者的Python快速教程
    为什么人工智能用Python
    计算机英语(一)
    Java 基础生词表
  • 原文地址:https://www.cnblogs.com/dengbiao/p/12409560.html
Copyright © 2011-2022 走看看