泛型之在方法体内使用泛型参数

package fft.generics;

//在方法体内使用泛型参数
public class Util {
    
    //该方法用于比较两个Pair对象是否相等。  
    //泛型参数必须写在方法返回类型boolean之前  
        public static <K, V> boolean compare(Pair<K,V> p1, Pair<K, V> p2) {  
            return p1.getKey().equals(p2.getKey())&&  
                  p1.getValue().equals(p2.getValue());  
        }  
    
        
        public static void main(String[] args) {
            Pair<Integer,String> p1 = new OrderPair<Integer,String>(1, "apple");  
            Pair<Integer,String> p2 = new OrderPair<Integer,String>(2, "pear");  
            boolean same = Util.<Integer, String>compare(p1, p2);

            System.out.println("same="+same);
            
        }
      
发布了195 篇原创文章 · 获赞 52 · 访问量 31万+

猜你喜欢

转载自blog.csdn.net/hongweideng/article/details/53839972
今日推荐