package generic; public class PairG<T> { T first; T 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; } public static void main(String[] args) { getPrintBuddy(new PairG<Employee>()); // getPrintBuddy(new PairG<Manager>());//error getPrintBuddyE(new PairG<Employee>()); getPrintBuddyE(new PairG<Manager>()); getPrintBuddyS(new PairG<Employee>()); getPrintBuddyS(new PairG<Manager>()); } //不能将参数类型传到静态方法 // public static T void method(T t){}//error //不能将 PairG<Manager> m 传递给这个方法 public static void getPrintBuddy(PairG<Employee> p){ Employee p1 = p.getFirst(); p.setFirst(new Employee()); } public static void getPrintBuddyE(PairG<? extends Employee> p){ //安全的访问器 Employee p1 = p.getFirst(); //编译器只知道Employee的类型,不知道具体的类型,所以拒绝传递任何特定的类型 // p.setFirst(new Employee()); //error } public static void getPrintBuddyS(PairG<? super Manager> p){ // Manager p1 = p.getFirst(); //error //安全的设置器 p.setFirst(new Manager()); } }