黑马程序员技术交流社区
标题:
求指导使用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类中自己定义的成员隐藏了
public int compareTo(Person o) {
复制代码
这是访问父类中的成员属性,所以出现这个错误。
正确代码。
import java.util.Iterator;
import java.util.TreeSet;
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<T_Person2>
{
String name;
int age;
public T_Person2(String name, int age) {
super();
this.name = name;
this.age = age;
}
public T_Person2(){}
public int compareTo(T_Person2 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 ComparableClass {
public static void main(String[] args) {
TreeSet<T_Person2> p = new TreeSet<T_Person2>();
p.add(new T_Person2("ee",11));
p.add(new T_Person2("ss",12));
p.add(new T_Person2("ee",11));
print(p);
}
public static void print(TreeSet<? extends T_Person2> a)
{
Iterator<? extends T_Person2> it = a.iterator();
while(it.hasNext())
{
T_Person2 t_Person = it.next();
System.out.println("age:"+t_Person.getAge()+" name:"+t_Person.getName());
}
}
}
复制代码
作者:
海饼干
时间:
2014-12-2 16:39
程序员中的"特种兵"基地
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2