黑马程序员技术交流社区

标题: 求指导使用Comparable时候发现的一个问题······不解今晚睡不... [打印本页]

作者: 黑小强    时间: 2014-12-2 12:04
标题: 求指导使用Comparable时候发现的一个问题······不解今晚睡不...
问题描述:  为什么  子类 实现Comparable 接口后~~~在Comparable后添加泛型<父类>   
实现方法 comparaTo(父类   o) --->这个o 引用输出的变量值都是空的
思考: 想法是这不是   父类 o  = new 子类  多态吗?  困惑了····  求指导


class People
{
    String name;
    int age;
   
    public People(String name, int age) {
        super();
        this.name = name;
        this.age = age;
    }
    public People(){}
   
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
   
}
class T_Person2  extends People implements Comparable<;People>   
{
    String name;
    int age;
   
    public T_Person2(String name, int age) {
        super();
        this.name = name;
        this.age = age;
    }
    public T_Person2(){}
   
   
    public int compareTo(People o) {                              
        System.out.println("T_Person=========");
        System.out.println("ta:"+this.age +" oa:"+o.age);  //问题主要在这~~~   ta:11 oa:0    为什么 oa 输出结果一直都是0,
                                                                                           //People o 不        能获得对象?
        if(this.age == o.age)
        {
            String tN = this.name;
            String oN = o.name;
            System.out.println("tn:"+tN +" oN:"+oN);
            return tN.compareTo(oN);
        }
        return new Integer(this.age).compareTo(new Integer(o.age));
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
   
}
public class Test_2泛型 {
    public static void main(String[] args) {
   
        TreeSet<T_Person> p = new TreeSet<T_Person>();
        
        p.add(new T_Person("ee",11));
        p.add(new T_Person("ss",12));
        p.add(new T_Person("ee",11));
        
        print(p);
    }
    public static void print(TreeSet<? extends T_Person> a)
    {
        Iterator<? extends T_Person>  it = a.iterator();
        
        while(it.hasNext())
        {
            T_Person t_Person = it.next();
            System.out.println("age:"+t_Person.getAge()+"  name:"+t_Person.getName());
        }
    }
}



作者: kerner    时间: 2014-12-2 14:31
本帖最后由 kerner 于 2014-12-2 15:34 编辑

以后发帖记得用代码格式,这样会容易让别人阅读。
首先,你的代码好多都是错误的啊,这都不能被编译过。
问题在于 T_Person2继承Person,那么T_Person2类中也具有Person的成员,但是它被T_perosn2类中自己定义的成员隐藏了
  1.   public int compareTo(Person o) {   
复制代码

这是访问父类中的成员属性,所以出现这个错误。
正确代码。
  1. import java.util.Iterator;
  2. import java.util.TreeSet;

  3. class People
  4. {
  5.     String name;
  6.     int age;
  7.    
  8.     public People(String name, int age) {
  9.         super();
  10.         this.name = name;
  11.         this.age = age;
  12.     }
  13.     public People(){}
  14.    
  15.     public String getName() {
  16.         return name;
  17.     }
  18.     public void setName(String name) {
  19.         this.name = name;
  20.     }
  21.     public int getAge() {
  22.         return age;
  23.     }
  24.     public void setAge(int age) {
  25.         this.age = age;
  26.     }
  27.    
  28. }
  29. class T_Person2  extends People implements Comparable<T_Person2>
  30. {
  31.     String name;
  32.     int age;
  33.    
  34.     public T_Person2(String name, int age) {
  35.         super();
  36.         this.name = name;
  37.         this.age = age;
  38.     }
  39.     public T_Person2(){}
  40.    
  41.    
  42.     public int compareTo(T_Person2 o) {                              
  43.         System.out.println("T_Person=========");
  44.         System.out.println("ta:"+this.age +" oa:"+o.age);  //问题主要在这~~~   ta:11 oa:0    为什么 oa 输出结果一直都是0,
  45.                                                                                            //People o 不        能获得对象?
  46.         if(this.age == o.age)
  47.         {
  48.             String tN = this.name;
  49.             String oN = o.name;
  50.             System.out.println("tn:"+tN +" oN:"+oN);
  51.             return tN.compareTo(oN);
  52.         }
  53.         return new Integer(this.age).compareTo(new Integer(o.age));
  54.     }
  55.     public String getName() {
  56.         return name;
  57.     }
  58.     public void setName(String name) {
  59.         this.name = name;
  60.     }
  61.     public int getAge() {
  62.         return age;
  63.     }
  64.     public void setAge(int age) {
  65.         this.age = age;
  66.     }
  67.    
  68. }
  69. public class ComparableClass {
  70.     public static void main(String[] args) {
  71.    
  72.         TreeSet<T_Person2> p = new TreeSet<T_Person2>();
  73.         
  74.         p.add(new T_Person2("ee",11));
  75.         p.add(new T_Person2("ss",12));
  76.         p.add(new T_Person2("ee",11));
  77.         
  78.         print(p);
  79.     }
  80.     public static void print(TreeSet<? extends T_Person2> a)
  81.     {
  82.         Iterator<? extends T_Person2>  it = a.iterator();
  83.         
  84.         while(it.hasNext())
  85.         {
  86.             T_Person2 t_Person = it.next();
  87.             System.out.println("age:"+t_Person.getAge()+"  name:"+t_Person.getName());
  88.         }
  89.     }
  90. }
复制代码




作者: 海饼干    时间: 2014-12-2 16:39
程序员中的"特种兵"基地




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2