A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

本帖最后由 李勇 于 2012-7-17 15:07 编辑

package day15;
import java.util.*;
class Students implements Comparable//该接口强制让学生类具备比较性
{
private String name;
private int age;

Students(String name,int age)
{
  this.name = name;
  this.age  = age;
}
public int compareTo(Object obj)
{
  //return 1;   二叉树原理  升序   
  //return 0;   二叉树原理   只打印一个
  //return -1;  二叉树原理 降序  
  if(!(obj instanceof Students))
    throw new RuntimeException("你不是学生");
  Students s = (Students)obj;
  
  //System.out.println(this.name+"... Comparable 相比较"+s.name);
  if(this.age>s.age)//可以写成 当前年龄减去对象年龄 如果是正数就是正数 如果是负数就是负数
   return 1;
  if(this.age==s.age)//如果比较的年龄相同就返回0  意思就是 比较的年龄相同 只存一个进来
  {
   return this.name.compareTo(s.name);
  }
  return -1;
}
public int getAge()
{
  return age;
}
public String getName()
{
  return name;
}
}
public class 实现Comparator方式排序
{
public static void sop(Object obj)
{
  System.out.println(obj);
}
public static void main(String[] args)
{
  TreeSet ts = new TreeSet(new MyCompare());
  //比如的是姓名排序
  
  ts.add(new Students("lisi02",22));
  ts.add(new Students("lisi007",20));
  ts.add(new Students("lisi09",19));
  ts.add(new Students("lisi06",18));
  ts.add(new Students("lisi007",29));
  //ts.add(new Student("lisi01",40));
  /*
  ts.add("cba");
  ts.add("abcd");
  ts.add("aaa");
  ts.add("bca");
  ts.add("Fbcd");
  */
  Iterator it = ts.iterator();
  while(it.hasNext())
  {
   Students stu = (Students)it.next();
   sop(stu.getName()+"..."+stu.getAge());
  }
}
}
class MyCompare implements Comparator//比较的是姓名排序
{
public int compare(Object o1,Object o2)
{
  Student s1 = (Student)o1;
  Student s2 = (Student)o2;
  int num = s1.getName().compareTo(s2.getName());
  if(num == 0)//容易理解的if方法
  {
   return new Integer(s1.getAge()).compareTo(new Integer(s2.getAge()));
  }
  return num;
}
}


错误提示:
Exception in thread "main" java.lang.ClassCastException: day15.Students cannot be cast to day15.Student
at day15.MyCompare.compare(实现Comparator方式排序.java:88)
at java.util.TreeMap.compare(Unknown Source)
at java.util.TreeMap.put(Unknown Source)
at java.util.TreeSet.add(Unknown Source)
at day15.实现Comparator方式排序.main(实现Comparator方式排序.java:62)

10 个回复

倒序浏览
学生类定义的时候写的是Students
MyCompare里面写的是Student
应该是day15包里面在别的java文件中还定义了Student类
所以编译没有出问题 但是转换的时候就抛异常了

评分

参与人数 1技术分 +1 收起 理由
韦念欣 + 1 赞一个!

查看全部评分

回复 使用道具 举报
import java.util.*;
class Students implements Comparable//该接口强制让学生类具备比较性
{
private String name;
private int age;

Students(String name,int age)
{
  this.name = name;
  this.age  = age;
}
public int compareTo(Object obj)
{
  //return 1;   二叉树原理  升序   
  //return 0;   二叉树原理   只打印一个
  //return -1;  二叉树原理 降序  
  if(!(obj instanceof Students))
    throw new RuntimeException("你不是学生");
  Students s = (Students)obj;
  
  //System.out.println(this.name+"... Comparable 相比较"+s.name);
  if(this.age>s.age)//可以写成 当前年龄减去对象年龄 如果是正数就是正数 如果是负数就是负数
   return 1;
  if(this.age==s.age)//如果比较的年龄相同就返回0  意思就是 比较的年龄相同 只存一个进来
  {
   return this.name.compareTo(s.name);
  }
  return -1;
}
public int getAge()
{
  return age;
}
public String getName()
{
  return name;
}
}
public class StudentsTest//Comparator方式排序
{
public static void sop(Object obj)
{
  System.out.println(obj);
}
public static void main(String[] args)
{
  TreeSet ts = new TreeSet(new MyCompare());
  //比如的是姓名排序
  
  ts.add(new Students("lisi02",22));
  ts.add(new Students("lisi007",20));
  ts.add(new Students("lisi09",19));
  ts.add(new Students("lisi06",18));
  ts.add(new Students("lisi007",29));
  //ts.add(new Student("lisi01",40));
  /*
  ts.add("cba");
  ts.add("abcd");
  ts.add("aaa");
  ts.add("bca");
  ts.add("Fbcd");
  */
  Iterator it = ts.iterator();
  while(it.hasNext())
  {
   Students stu = (Students)it.next();
   sop(stu.getName()+"..."+stu.getAge());
  }
}
}
class MyCompare implements Comparator//比较的是姓名排序
{
public int compare(Object o1,Object o2)
{
  Students s1 = (Students)o1;
  Students s2 = (Students)o2;

  int num = s1.getName().compareTo(s2.getName());
  if(num == 0)//容易理解的if方法
  {
   return new Integer(s1.getAge()).compareTo(new Integer(s2.getAge()));
  }
  return num;
}
}
红色部分都是修改后的部分 修改完运行一切OK  LZ  你是在考坛友们的代码阅读能力吗?

评分

参与人数 1技术分 +1 收起 理由
韦念欣 + 1 赞一个!

查看全部评分

回复 使用道具 举报
如果我没有看错的话,你这代码里面牵涉到代码转型的问题,也就是多态中的一个知识点:
比如:
Dog d = new Dog();//d只能指向狗对象。可以调用狗的所有功能。

Animal a = new Dog();//a可以指向任何动物对象。只能调用Animal类的方法。子类对象的类型提升。向上转型。
a.eat();
好处:提高了扩展性,局限:只能使用父类中的方法。
这种提升可以限制对子类对象的操作。

Dog d = (Dog)a;//向下转型。转成子类型。
d.eat();
d.lookHome();


什么时候使用向上转型,和向下转型呢?
当需要对程序进行扩展,或者限定对对象的方法操作时,使用向上转型。操作其父类型。
当要使用子类的特有的内容时,就需要向下转型。
转型前一定要判断,否则容易出现问题。

注意:在这个转型过程中,自始至终都是一个子类对象在做着类型的转变而已。
千万别把父类对象转成子类型,那是不可能的。

Animal a=new Animal();
Dog d = (Dog)a;//这种写法是绝对错误的~

Animal a=new Dog();
Dog d = (Dog)a;//这种是可以的。

把这个看明白,你的疑问也就迎刃而解了!希望对你有所帮助哟!
               

点评

拜托,没那么复杂。。。。  发表于 2012-7-16 21:44
回复 使用道具 举报
楼上正解。建议楼主加上泛型,就会避免类似的问题。
回复 使用道具 举报
本帖最后由 陆强强 于 2012-7-16 20:00 编辑

class Students implements Comparable//这里的类名是Students
到了比较器就
class MyCompare implements Comparator{
        public int compare(Object o1,Object o2)
        {
                    Student s1 = (Student)o1;//这里变成了Student  
                  Student s2 = (Student)o2;
还有就是楼主注意下类名
class 实现Comparator方式排序
尽量不要用中文,首字母大写

评分

参与人数 1技术分 +1 收起 理由
韦念欣 + 1 赞一个!

查看全部评分

回复 使用道具 举报
李勇 初级黑马 2012-7-16 19:59:48
7#
温少邦 发表于 2012-7-16 19:37
学生类定义的时候写的是Students
MyCompare里面写的是Student
应该是day15包里面在别的java文件中还定义了S ...

哈哈  我发现了 是包的问题 其他的类里 已经有了Student了 所以 我在部分代码加了 s 却不再后面的main函数里 加   但是也不会报错 只会出现 编译错误
回复 使用道具 举报
李勇 初级黑马 2012-7-16 20:01:15
8#
achilles 发表于 2012-7-16 19:46
import java.util.*;
class Students implements Comparable//该接口强制让学生类具备比较性
{

哈哈 被你发现了 我是在考代码阅读能力 ~   非常感谢你的代码
回复 使用道具 举报
李勇 初级黑马 2012-7-16 20:02:13
9#
曹魁 发表于 2012-7-16 19:48
如果我没有看错的话,你这代码里面牵涉到代码转型的问题,也就是多态中的一个知识点:
比如:
Dog d = new  ...

类型转换异常 是我牵连到其他类 才会出现的问题  多态我明白的 谢谢你的总结
回复 使用道具 举报
陆强强 发表于 2012-7-16 19:59
class Students implements Comparable//这里的类名是Students
到了比较器就
class MyCompare implements C ...

啊哈哈~ 前辈见笑了  小生 将练习主类名字定义为 中文 是因为我需要中文标签来方便以后观看与扩展 还有调用 以及 后人方便查看

点评

可以把文件名定义中文,类名英文  发表于 2012-7-17 08:36
回复 使用道具 举报
import java.util.*;
class Students implements Comparable
{
        private String name;
        private int age;

        Students(String name,int age)
        {
           this.name = name;
           this.age  = age;
         }
         public int compareTo(Object obj)
         {  
                if(!(obj instanceof Students))
                        throw new RuntimeException("你不是学生");
                Students s = (Students)obj;
           if(this.age>s.age)
                        return 1;
           if(this.age==s.age)//如果比较的年龄相同就返回0  意思就是 比较的年龄相同 只存一个进来
           {
                        return this.name.compareTo(s.name);
           }
                return -1;
         }
         public int getAge()
         {
           return age;
         }
         public String getName()
         {
           return name;
         }
}
public class Test1
{
        public static void sop(Object obj)
        {
                 System.out.println(obj);
    }
        public static void main(String[] args)
        {
                TreeSet ts = new TreeSet(new MyCompare());
                //比如的是姓名排序
  
                ts.add(new Students("lisi02",22));
                ts.add(new Students("lisi007",20));
                ts.add(new Students("lisi09",19));
                ts.add(new Students("lisi06",18));
                ts.add(new Students("lisi007",29));
                Iterator it = ts.iterator();
                while(it.hasNext())
                {
                        Students stu = (Students)it.next();
                        sop(stu.getName()+"..."+stu.getAge());
                }
        }
}
class MyCompare implements Comparator//比较的是姓名排序
{
        public int compare(Object o1,Object o2)
        {
                Students s1 = (Students)o1;
                Students s2 = (Students)o2;
                int num = s1.getName().compareTo(s2.getName());
                if(num == 0)//容易理解的if方法
                {
                         return new Integer(s1.getAge()).compareTo(new Integer(s2.getAge()));
                }
                return num;
        }
}


经过运行正确,你下面的Mycompare中的函数Student应该写成Students,可以直接用泛型,下面是试用泛型的代码
import java.util.*;
class Students implements Comparable
{
        private String name;
        private int age;

        Students(String name,int age)
        {
           this.name = name;
           this.age  = age;
         }
         public int compareTo(Object obj)
         {  
                if(!(obj instanceof Students))
                        throw new RuntimeException("你不是学生");
                Students s = (Students)obj;
           if(this.age>s.age)
                        return 1;
           if(this.age==s.age)//如果比较的年龄相同就返回0  意思就是 比较的年龄相同 只存一个进来
           {
                        return this.name.compareTo(s.name);
           }
                return -1;
         }
         public int getAge()
         {
           return age;
         }
         public String getName()
         {
           return name;
         }
}
public class Test1
{
        public static void sop(Object obj)
        {
                 System.out.println(obj);
    }
        public static void main(String[] args)
        {
                TreeSet<Students> ts = new TreeSet<Students>(new MyCompare());
                //比如的是姓名排序
  
                ts.add(new Students("lisi02",22));
                ts.add(new Students("lisi007",20));
                ts.add(new Students("lisi09",19));
                ts.add(new Students("lisi06",18));
                ts.add(new Students("lisi007",29));
                Iterator<Students> it = ts.iterator();
                while(it.hasNext())
                {
                        Students stu = (Students)it.next();
                        sop(stu.getName()+"..."+stu.getAge());
                }
        }
}
class MyCompare implements Comparator<Students>//比较的是姓名排序
{
        public int compare(Students s1,Students s2)
        {
//                Students s1 = (Students)o1;
//                Students s2 = (Students)o2;
                int num = s1.getName().compareTo(s2.getName());
                if(num == 0)//容易理解的if方法
                {
                         return new Integer(s1.getAge()).compareTo(new Integer(s2.getAge()));
                }
                return num;
        }
}


回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马