日期:2014-05-20  浏览次数:20728 次

请帮我解惑程序中的一些问题
下面的程序是基本按书上写的,也成功地运行了,就是有点不懂的地方在程序中注释出来了,希望能够帮我解a 惑。

import java.util.*;

public class Test
{
public static void main(String[] args)
{
Manager a = new Manager("abc", 6000, 2003, 11, 15);
Manager b = new Manager("efg", 7000, 2002, 10, 13);

A<Manager> c = new A<Manager>(a, b);
printC(c);

a.setBonus(5000);
b.setBonus(3000);
Manager[] managers = {a, b};         
        A<Employee> result = new A<Employee>(managers[0], managers[1]);
        //上面一行写成 A<Manager> result = new A<Manager>(managers[0], managers[1]);不行吗?
        minmaxBonus(managers, result);
        System.out.println(result.getFirst().getName());        
}

public static void printC(A<? extends Employee> p)//这里写成A<Employee> p不成吗?
    {
     Employee first = p.getFirst();
     Employee second = p.getSecond();
     System.out.println(first.getName() + second.getName());
    }
    
    public static void minmaxBonus(Manager[] x, A<? super Manager> y)//这里写成A<Manager> y不成吗?
    {
     if(x==null||x.length==0 )
     {
     return;
     }
     Manager min = x[0];
     Manager max = x[0];
     for(int i=1; i<x.length; i++)
     {
     if(min.getBonus() >x[i].getBonus())
     {
     min = x[i];
     }
     if(max.getBonus() <x[i].getBonus())
     {
     max = x[i];
     }
     }
    
     y.setFirst(min);
     y.setSecond(max);
    }
}

class A<T>
{
public A(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;
}

class B
{
public static boolean hasNull(A<?> p) //这里为什么不能用A<T> P
{
return p.getFirst()==null || p.getSecond()==null;
}

public static void swap(A<?> p)////这里为什么不能用A<T> P
{
swapHelper(p);
}

public static <T> void swapHelper(A<T> p)//这里为什么不用A<?> 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;
}

private String name;
private double salary;
private Date hireDay;
}

class Manager extends Employee
{
public Manager(String n, double s, int year, int month, int day)
{
super(n, s, year, month, day);
donus = 0;
}

public void setBonus(double b)
{
donus = b;
}

public double getBonus()
{
return donus;
}

public double getSalary()
{
return super.getSalary() + donus;
}

private double donus;
}