TA的每日心情 | 开心 2021-3-12 23:18 |
---|
签到天数: 2 天 [LV.1]初来乍到
|
一、定义范型类- /**
- @author Cay Horstmann
- */
- public class Pair< T> //范型类,T是类型参数,T只能是类类型
- {
- public Pair() { first = null; second = null; }
- public Pair(T first, T second) {
- this.first = first; this.second = second;
- }
- public T getFirst() { return first; }
- public T getSecond() { return second; }
- public void setFirst(T newValue) { first = newValue; }
- public void setSecond(T newValue) { second = newValue; }
- private T first;
- private T second;
- }
复制代码
二、测试一- public class PairTest1
- {
- public static void main(String[] args)
- {
- String[] words = { "Mary", "had", "a", "little", "lamb" };
- Pair< String> mm = ArrayAlg.minmax(words);//字符串版本的范型
- System.out.println("min = " + mm.getFirst());
- System.out.println("max = " + mm.getSecond());
- }
- }
- class ArrayAlg
- {
- /**
- Gets the minimum and maximum of an array of strings.
- @param a an array of strings
- @return a pair with the min and max value, or null if a is
- null or empty
- */
- public static Pair< String> minmax(String[] a)
- {
- if (a == null || a.length == 0) return null;
- String min = a[0];
- String 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< String>(min, max);
- }
- }
- 结果:
- C:java>java PairTest1
- min = Mary
- max = little
- 三、测试二
- import java.util.*;
- public class PairTest2
- {
- public static void main(String[] args)
- {
- GregorianCalendar[] birthdays =
- {
- new GregorianCalendar(1906, Calendar.DECEMBER, 9), // G. Hopper
- new GregorianCalendar(1815, Calendar.DECEMBER, 10), // A. Lovelace
- new GregorianCalendar(1903, Calendar.DECEMBER, 3), // J. von Neumann
- new GregorianCalendar(1910, Calendar.JUNE, 22), // K. Zuse
- };
- Pair< GregorianCalendar> mm = ArrayAlg.minmax(birthdays);//日期版的范型
- System.out.println("min = " + mm.getFirst().getTime());
- System.out.println("max = " + mm.getSecond().getTime());
- }
- }
- class ArrayAlg
- {
- /**
- Gets the minimum and maximum of an array of objects of type T.
- @param a an array of objects of type T
- @return a pair with the min and max value, or null if a is
- null or empty
- */
- 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);
- }
- }
- 运行结果:
- C:java>java PairTest2
- min = Sun Dec 10 00:00:00 CST 1815
- max = Wed Jun 22 00:00:00 CST 1910
- 四、测试三
- import java.util.*;
- public class PairTest3
- {
- public static void main(String[] args)
- {
- Manager ceo = new Manager("Gus Greedy", 800000, 2003, 12, 15);
- Manager cfo = new Manager("Sid Sneaky", 600000, 2003, 12, 15);
- Pair< Manager> buddies = new Pair< Manager>(ceo, cfo);//管理人员版的范型
- printBuddies(buddies);
- ceo.setBonus(1000000);
- cfo.setBonus(500000);
- Manager[] managers = { ceo, cfo };
- Pair< Employee> result = new Pair< Employee>();//雇员版的范型
- minMaxBonus(managers, result);
- System.out.println("first: " + result.getFirst().getName()
- + ", second: " + result.getSecond().getName());
- maxMinBonus(managers, result);
- System.out.println("first: " + result.getFirst().getName()
- + ", second: " + result.getSecond().getName());
- }
- public static void printBuddies(Pair< ? extends Employee> p)//指定上界,只有Employee或其子类才是可接受的参数
- {
- Employee first = p.getFirst();
- Employee second = p.getSecond();
- System.out.println(first.getName() + " and " + second.getName() + " are buddies.");
- }
- public static void minMaxBonus(Manager[] a,Pair< ? super Manager> result)//指定下界,只有Manager的超类才是可接受的参数
- {
- if (a == null || a.length == 0) return;
- Manager min = a[0];
- Manager max = a[0];
- for (int i = 1; i < a.length; i++)
- {
- if (min.getBonus() > a[i].getBonus()) min = a[i];
- if (max.getBonus() < a[i].getBonus()) max = a[i];
- }
- result.setFirst(min);
- result.setSecond(max);
- }
- public static void maxMinBonus(Manager[] a, Pair< ? super Manager> result)//指定了下界
- {
- minMaxBonus(a, result);
- PairAlg.swapHelper(result); // OK--swapHelper captures wildcard type
- }
- }
- class PairAlg
- {
- public static boolean hasNulls(Pair< ?> p)//通配符参数"?"表示未知类型,Pair< ?>可以匹配任一Pair对象
- {
- return p.getFirst() == null || p.getSecond() == null;
- }
- public static void swap(Pair< ?> p) { swapHelper(p); }
- public static < T> void swapHelper(Pair< T> p)
- {
- T t = p.getFirst();
- p.setFirst(p.getSecond());
- p.setSecond(t);
- }
- }
- //雇员类
- class Employee
- {
- public Employee(String n, double s, int year, int month, int day)
- {
- name = n;//姓名
- salary = s;//薪水
- GregorianCalendar calendar = new GregorianCalendar(year, month - 1, day);
- hireDay = calendar.getTime();//受雇的日期
- }
- public String getName()
- {
- return name;
- }
- public double getSalary()
- {
- return salary;
- }
- public Date getHireDay()
- {
- return hireDay;
- }
- public void raiseSalary(double byPercent)
- {
- double raise = salary * byPercent / 100;
- salary += raise;
- }
- private String name;
- private double salary;
- private Date hireDay;
- }
- //管理人员类
- class Manager extends Employee
- {
- /**
- @param n the employee"s name
- @param s the salary
- @param year the hire year
- @param month the hire month
- @param day the hire day
- */
- public Manager(String n, double s, int year, int month, int day)
- {
- super(n, s, year, month, day);
- bonus = 0;
- }
- public double getSalary()
- {
- double baseSalary = super.getSalary();
- return baseSalary + bonus;
- }
- public void setBonus(double b)
- {
- bonus = b;
- }
- public double getBonus()
- {
- return bonus;
- }
- private double bonus;//奖金
- }
- 运行结果:
- C:java>java PairTest3
- Gus Greedy and Sid Sneaky are buddies.
- first: Sid Sneaky, second: Gus Greedy
- first: Gus Greedy, second: Sid Sneaky
-
复制代码
源码下载:http://file.javaxxz.com/2014/10/30/235642656.zip |
|