ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

Java面向对象程序设计--泛型编程

2019-08-04 12:09:08  阅读:237  来源: 互联网

标签:Java Pair newValue 面向对象 second 泛型 return public first


原文链接:http://www.cnblogs.com/jiangheng/p/3761865.html

1. 为何要进行泛型编程?

泛型变成为不同类型集合提供相同的代码!省去了为不同类型而设计不同代码的麻烦!

 

2. 一个简单泛型类的定义:


 1 public class PairTest1 
 2 {
 3     public static void main(String[] args) 
 4     {
 5         String[] arr = {"This","is","the","end","of","the","world"};
 6         Pair<String> mm = ArrayAlg.minmax(arr);
 7         System.out.println("min = " + mm.getFirst());
 8         System.out.println("max = " + mm.getSecond());
 9     }
10 }
11 
12 class Pair<T>
13 {
14     public Pair() {first = null; second = null;}
15     public Pair(T first,T second) {this.first = first; this.second = second;}
16     
17     public T getFirst() {return first;}
18     public T getSecond() {return second;}
19 
20     public void setFirst(T newValue) {this.first = newValue;}
21     public void setSecond(T newValue) {this.second = newValue;}
22     
23     private T first;
24     private T second;
25 }
26 
27 class ArrayAlg
28 {
29     public static Pair<String> minmax(String[] a)
30     {
31         if(a == null || a.length == 0) return null;
32         String min = a[0];
33         String max = a[0];
34         for(int i = 1; i < a.length; i++)
35         {
36             if(min.compareTo(a[i]) > 0) min = a[i];
37             if(max.compareTo(a[i]) < 0) max = a[i];
38         }
39         return new Pair<String>(min,max);
40     }
41 }

上面这段代码中定义了一个泛型类。

 

2. 泛型函数:

下面是一个泛型函数定义的实例:

1 class ArrayAlg
2 {    
3     public static <T> T getMiddle(T[] a)
4     {
5         return a[a.length / 2];
6     }
7 }

注意,一个泛型函数可以定义在一个普通类中,也可以定义在一个泛型类中。

 注意在上述模板函数的定义中,模板参数变量的位置,是出现在public static 修饰符之后的!

下面是模板函数的调用:

        String[] arr = {"this","is","me"};
        String middle = ArrayAlg.<String>getMiddle(arr);
        System.out.println(middle);

在实际中,调用泛型函数时并不需要在函数前加上<String>操作符。java会自动进行类型检查,比如上面getMiddle会自动检查出参数arr的类型是String[]的,这样推测出

T的类型是String。

 

3. 类型变量的约束:

public static <T extends Comparable> Pair<T> minmax(T[] a)
    {
        if(a == null || a.length == 0) return null;
        T min = a[0];
        T max = a[0];
        for(int i = 1; i < a.length; i++)
        {
            if(min.compareTo(a[i]) > 0) min = a[i];
            if(max.compareTo(a[i]) < 0) max = a[i];
        }
        return new Pair<T>(min,max);
    }

这里为类型变量T添加了约束T extends Comparable,要求T实现Comarable接口中的CompareTo函数!

Java中泛型变量的约束条件是: <T extends BoundingType>, 你可以让T继承一个类,或者继承任意一个接口,或者同时继承一个类和一个接口,但无论如何

不能继承多个类!

 

4.泛型代码和虚拟机之间的关系:

虚拟机中没有泛型类型的对象,所有的对象都是具体的类型的对象。

当定义了一个泛型类型之后,编译器会自动提供一个相对应的原始类型(raw type):

raw type的类型名和泛型类型名相同,只是泛型类型参数被去掉了。类型变量被替换成成限定类型对象或Object类型对象。

 1 class Pair<T>
 2 {
 3     public Pair() {first = null; second = null;}
 4     public Pair(T first,T second) {this.first = first; this.second = second;}
 5     
 6     public T getFirst() {return first;}
 7     public T getSecond() {return second;}
 8 
 9     public void setFirst(T newValue) {this.first = newValue;}
10     public void setSecond(T newValue) {this.second = newValue;}
11     
12     private T first;
13     private T second;
14 }

上面的类型对应的原始类型是:

class Pair<T>
{
    public Pair() {first = null; second = null;}
    public Pair(Object first,Object second) {this.first = first; this.second = second;}
    
    public Object getFirst() {return first;}
    public Object getSecond() {return second;}

    public void setFirst(Object newValue) {this.first = newValue;}
    public void setSecond(Object newValue) {this.second = newValue;}
    
    private Object first;
    private Object second;
}

  

 




 

转载于:https://www.cnblogs.com/jiangheng/p/3761865.html

标签:Java,Pair,newValue,面向对象,second,泛型,return,public,first
来源: https://blog.csdn.net/weixin_30502157/article/details/98452741

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有