Java 之泛型

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/woxueplc/article/details/80525414

一. 为什么会有泛型:
       我们知道在代码中,数据是有类型的,以此为基础,处理该数据的函数也是需要添加类型限制的,比如下面这个例子。

public class MyClass {
    public static void main(String[] args){
        int boyAccount = 10;
        int girlAccount = 15;
        int allAccount = count(boyAccount, girlAccount);
    }

    public static int count(int boy, int girl) {
        return boy +girl;
    }
}
    对于count函数来说,其规定了其传入的实参必须是整形,传出的结果也是整形。如果我们需要用它来计算两个double类型的,或者一个double类型,一个long类型的的实参的和,最后我们就会看到类似下面的错误:
Error:(8, 43) : error: incompatible types: possible lossy conversion from double to int
这就是典型的类型不匹配问题。代码已经被写的死死的了,没办法再复用了。同样,这样的问题也会出现在各种类,各种接口,各种工具的使用和开发中,这非常影响代码的质量(比如复用性,扩展性等等)。

二. 泛型的基本思想:
    泛型是各类计算机编程语言中非常常见一种形式。其主要思想为通过把代码的逻辑功能和数据类型分开,以这种形式来扩展代码的应用范围

三. 泛型的基本使用
      主要分为三种情况,泛型类,泛型接口,泛型方法

3.1 泛型类:
      Demo:

public class myClass {
    public static void main(String[] args){
        Height<Integer> heightI = new Height<Integer>(3);
        System.out.println("Integer  heightI ="+heightI.getHeight());
        Height<Double> heightD = new Height<Double>(3.14);
        System.out.println("Float  heightD ="+heightD.getHeight());
        Height<Float> heightF = new Height<Float>(3.14f);
        System.out.println("Float  heightF ="+heightF.getHeight());
    }
    /*1.<E>表示泛型类,E 是代表类型的一个变量,平时也可以用K,V,M等等替代,可以任意选择变量*/
    /*2.W外部指定了E的具体类型,如果<Integer>, 那么类中所有用E表示的类型,均用Integer替代*/
    /*3.泛型中的E变量只能是对象类型,不能是基本数据类型(4类整型byte,int,chort,long, 2种浮点型float,
      double, 一种boolean,一种字符char */
    public static class Height<E> {
        private E height;
        public Height(E e){
            this.height = e;
        }
        public void setHeight(E e) {
            this.height = e;
        }
        public E getHeight(){
            return height;
        }
    }
}

输出:Integer:  heightI =3
Float:  heightD =3.14
Float:  heightF =3.14
Process finished with exit code 0
泛型类是非常常见的,比如常见的容器,set, map, list, HashTable等等,均能看到它的身影,注意,如果 给泛型类制定具体的类型,其内部可以存储所有类型,例如Demo及其输出:
public class myClass {
    public static void main(String[] args){
        ArrayList temp = new ArrayList();
        temp.add(1);
        temp.add("Only you");
        temp.add(new Person(12));
        System.out.println("temp = "+temp);

    }

    public static class Person{
        public int age;
        public Person(int arg){
            age = arg;
        }
    }

}
输出:
temp = [1, Only you, com.example.testtwo.myClass$Person@75b84c92]

Process finished with exit code 0
在Demo中,我们在ArrayList 对象中存储了一个整形数据,一个字符串数据,一个类对象实例,这就是泛型类的强大之处。
3.2 泛型接口:
持续更新中。

3.3 泛型方法:
任务:完成( 一. 为什么会有泛型:)的例子, 如何用泛型实现任意两个数的和,这里的demo比较简单,仅供参考。
Demo:
泛型方法:
public class CountUtil {
    public <T extends Number> double countFunction(T a, T b) {
        double count;
        count = a.doubleValue()+b.doubleValue();
        return count;
    }
}
主函数:
public static void main(String[] args){
    CountUtil mUtil = new CountUtil();
    System.out.println("Count = "+mUtil.countFunction(3, 4.0));
    System.out.println("Count = "+mUtil.countFunction(3.8f, 4));
    System.out.println("Count = "+mUtil.countFunction(3.8232, 2.476f));
}
输出:
Count = 7.0
Count = 7.799999952316284
Count = 6.299200070571899

Process finished with exit code 0

四. 总结
持续更新中。


猜你喜欢

转载自blog.csdn.net/woxueplc/article/details/80525414