8-6 泛型

 

package generic;

import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.function.IntFunction;
import java.util.function.Supplier;

public class Pair<T> {
    
    T first;
    T second;
    
    public Pair() {
        super();
        
        //8.6.5
//        first = new T();//error
        
    }
    
    public Pair( T first ,T second) {
        super();
        
        //8.6.5
//        first = new T();//error
        
    }
    
    public static void main(String[] args) {
        
        
        //8.6.5不能实例化类型变量(泛型变量)
        
        //原因:泛型擦除会变成Object类型,不会 new Object()
        //构造函数引用
        //Supplier     无参数而且返回是T 的函数式接口
//        Pair<String> pair2 = new Pair<T>();  //error
        
        Pair<String> pair1 = Pair.makePair(String :: new);
        Pair<String> pair2 = Pair.makePair(String.class);
        System.out.println(pair1);
        
        //8.6.6 不能构造泛型数组
        
//        Pair<String> pair[] = new Pair<String>[3]; //error
        
        
//        ArrayLy.maxMin( "T","D","H");   //error 报错  [Ljava.lang.Object; cannot be cast to [Ljava.lang.Comparable;
//        ArrayLy.maxMin(String[] :: new, "T","D","H");
        
        ArrayLy.maxMinT("T","D","H");
        
    }
    
    
    
    public static <T> Pair<T> makePair(Supplier<T> conr){
        return new Pair<T>();
    }
    
    public static <T> Pair<T> makePair(Class<T> cls){
        try {
            return new Pair<T>(cls.newInstance(),cls.newInstance());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
    
    
    //不能构造一个泛型数组
    /*public static <T extends Comparable> T[] maxMin(){
//        T[] mm = new  T[2];   //error
    }*/
    
    
    
    
    
    static class ArrayLy<E>{
        
        private E[] element;

        public E[] getElement() {
            return element;
        }

        public void setElement(E[] element) {
            this.element = element;
        }
        
        public ArrayLy(){
            element = (E[]) new Object[10]; 
        }
        
        public static <T extends Comparable<T>> T[] maxMin(T ... ts){
            Object [] object = new Object[2];
            
            if (ts == null || ts.length == 0) {
                return null;
            }
            T min = ts[0];
            T max = ts[0];
            
            for(T t : ts){
                if (min.compareTo(t)>0) {
                    min = t;
                }
                if (max.compareTo(t)<0) {
                    max = t;
                }
            }
            object[0] = min;
            object[1] = max;
            
            return (T[]) object;
        }
        
        //修改1 ,构造器表达式
        public static <T extends Comparable<T>> T[] maxMin(IntFunction<T[]> conrs , T ... ts){
//            Object [] object = new Object[2];
            
            T[] object = conrs.apply(2);
            
            if (ts == null || ts.length == 0) {
                return null;
            }
            T min = ts[0];
            T max = ts[0];
            
            for(T t : ts){
                if (min.compareTo(t)>0) {
                    min = t;
                }
                if (max.compareTo(t)<0) {
                    max = t;
                }
            }
            object[0] = min;
            object[1] = max;
            
            return (T[]) object;
        }
        
        //修改2 反射
        @SafeVarargs 
        public static <T extends Comparable<T>> T[] maxMinT(T ... ts){
            T[] object =   (T[]) Array.newInstance(ts.getClass().getComponentType(), 2);
            
            if (ts == null || ts.length == 0) {
                return null;
            }
            T min = ts[0];
            T max = ts[0];
            
            for(T t : ts){
                if (min.compareTo(t)>0) {
                    min = t;
                }
                if (max.compareTo(t)<0) {
                    max = t;
                }
            }
            object[0] = min;
            object[1] = max;
            
            return (T[]) object;
        }
        
    }

    
}

 

posted @ 2018-02-06 13:24  aLa神灯  阅读(129)  评论(0编辑  收藏  举报