美文网首页
java 泛型extends和super的区别

java 泛型extends和super的区别

作者: 你好宝宝 | 来源:发表于2020-04-23 19:18 被阅读0次

    假设有Employee以及Manager两个类,Employee是Manager的父类。

    public class Employee {
        private String name;
        private double salary;
    
        public Employee(String name, double salary) {
            this.name = name;
            this.salary = salary;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public double getSalary() {
            return salary;
        }
    
        public void setSalary(double salary) {
            this.salary = salary;
        }
    }
    
    public class Manager extends Employee {
    
        public Manager(String name, double salary) {
            super(name, salary);
        }
        public void modifySalary(Employee e,double salary) {
            e.setSalary(salary);
        }
    }
    

    以及将两个员工进行配对的类Pair

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

    下面代码具体阐释二者区别,<? extentds ...>主要用于读,< ? super ...>主要用于写

    public class PairTest {
        public static void main(String[] args) {
            Manager ceo = new Manager("nike", 1000);
            Manager cfo = new Manager("tom", 2000);
            Pair<Manager> buddies = new Pair<>(ceo, cfo);
            printBuddies(buddies);
    
            Manager[] managers = { ceo, cfo };
            Pair<Employee> result = new Pair<>();
            minmaxSalary(managers, result);
            System.out
                    .println("first name:" + result.getFirst().getName() + ",second name:" + result.getSecond().getName());
            maxminSalary(managers, result);
            System.out
                    .println("first name:" + result.getFirst().getName() + ",second name:" + result.getSecond().getName());
        }
    
        /**
         * 这里确定上界,参数是Employee或者其子类
         * 
         * @param p
         */
        public static void printBuddies(Pair<? extends Employee> p) {
            Employee first = p.getFirst();
            Employee second = p.getSecond();
            System.out.println("first name:" + first.getName() + "  second name:" + second.getName());
    
        }
    
        /**
         * 这里确定下界,参数可以是Manager或者其父类
         * 
         * @param a
         * @param result
         */
        public static void minmaxSalary(Manager[] a, Pair<? super Manager> result) {
            if (a.length == 0)
                return;
    
            Manager min = a[0];
            Manager max = a[0];
    
            for (int i = 0; i < a.length; i++) {
                if (a[i].getSalary() > max.getSalary())
                    max = a[i];
                if (a[i].getSalary() < min.getSalary())
                    min = a[i];
            }
    
            result.setFirst(min);
            result.setSecond(max);
        }
    
        public static void maxminSalary(Manager[] a, Pair<? super Manager> result) {
            minmaxSalary(a, result);
            swap(result);
        }
    
        public static <T> void swap(Pair<T> p) {
            T t = p.getFirst();
            p.setFirst(p.getSecond());
            p.setSecond(t);
        }
    }
    

    相关文章

      网友评论

          本文标题:java 泛型extends和super的区别

          本文链接:https://www.haomeiwen.com/subject/wjxzihtx.html