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

为什么这个泛型程序没有输出?
import java.util.*;

public class Test2
{
public static void main(String[] args)
{
Employee a = new Employee("abc", 2000);
Employee b = new Employee("efg", 6000);

A<Employee> c = new A<Employee>(a ,b);  
}

public static void printC(A<? extends Employee> p)
    {
     Employee first = p.getFirst();
     Employee second = p.getSecond();
     System.out.println(first.getName() + second.getName());
    }
}

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 Employee
{
String name;
double salary;

public Employee(String name, double salary)
{
this.name = name;
this.salary = salary;
}

public String getName()
{
return name;
}
}


编译通过了,就是没有任何显示。我觉得应该输出abc 与efg呀。

------解决方案--------------------
你就没调打印方法能输出什么啊
------解决方案--------------------
没有调用printC这个静态方法,自然没有输出结果!
------解决方案--------------------
同意楼上,没有输出方法
------解决方案--------------------
你都没调用打印的方法。。
------解决方案--------------------
同上 main方法里面没有调用输出的方法