- import java.util.*;
- class Person
- {
- private String name;
- private int age;
- Person(String name, int age)
- {
- this.name = name;
- this.age = age;
- }
- public int getAge()
- {
- return age;
- }
- public String toString()
- {
- return "name= "+name+'\t'+"age= "+age;
- }
- }
- class Tools<T>
- {
- public T getMaxAge(T t1, T t2)
- {
- return t1.getAge() > t2.getAge() ? t1 : t2;
- }
- }
- class GenericDemo
- {
- public static void main(String[] args)
- {
- Tools<Person> tools = new Tools<Person>();
-
- Person p1 = new Person("Jack", 32);
- Person p2 = new Person("Kate", 18);
- System.out.println(tools.getMaxAge(p1, p2));
- }
- }
复制代码 只要已编译就会出现如下提示:
说是在类型为T的变量t1中找不到符号getAge(),T扩展已在类Tools中声明Object。
可我明明在创建Tools类时,指定了的泛型为Person,为什么还是无法调用t1,t2的特有方法呢?
|
|