黑马程序员技术交流社区

标题: 接口实现问题 [打印本页]

作者: ztwztw    时间: 2014-1-4 21:43
标题: 接口实现问题
import java.io.*;
import java.util.*;
import java.util.Map.Entry;

public class PersonText {
        public static void main(String[] args){
     TreeSet ts = new TreeSet();
     ts.add(new Student("zhawern",223));
     ts.add(new Student("zhaan",243));
     ts.add(new Student("zhandgan",23));
     ts.add(new Student("zhangaan",63));
        }
}
class Student implements Comparable
{
        private String name;
        private int age;
        Student(String name,int age){
                this.name = name;
                this.age =age;
        }
        public  String getName(){
                return name;
        }
        public int getAge(){
                return age;
        }
        public int comparaTo(Object obj){
                if(obj instanceof Student){
                         throw  new RuntimeException();
                        }
                Student s = (Student)obj;
                        return this.name.compareTo(s.name);
        }
       
        }
这个程序哪里出错误了
作者: hurryup    时间: 2014-1-4 21:51
  if(obj instanceof Student){
应当是不匹配该条件才抛异常即if(!obj instanceof Student)

作者: 李兴    时间: 2014-1-4 22:04
你的代码中comparaTo自定义方法
不是对Comparable接口的compareTo()方法的实现
所以报告接口方法compareTo没有实现错误
改为compareTo
  1. public int compare[color=Blue][/color]To(Object obj){
  2.                 if(obj instanceof Student){
  3.                                 throw  new RuntimeException();
  4.                 }
  5.                 Student s = (Student)obj;
  6.                 //return this.name.compareTo(s.name);
  7.                 return 0;
  8.         }
复制代码

作者: DOOR    时间: 2014-1-4 22:05
本帖最后由 DOOR 于 2014-1-4 22:09 编辑
  1. import java.io.*;
  2. import java.util.*;
  3. import java.util.Map.Entry;

  4. public class PersonText {
  5.         public static void main(String[] args){
  6.      TreeSet ts = new TreeSet();
  7.      ts.add(new Student("zhawern",223));
  8.      ts.add(new Student("zhaan",243));
  9.      ts.add(new Student("zhandgan",23));
  10.      ts.add(new Student("zhangaan",63));
  11.         }
  12. }
  13. class Student implements Comparable
  14. {
  15.         private String name;
  16.         private int age;
  17.         Student(String name,int age){
  18.                 this.name = name;
  19.                 this.age =age;
  20.         }
  21.         public  String getName(){
  22.                 return name;
  23.         }
  24.         public int getAge(){
  25.                 return age;
  26.         }
  27.         
  28.                
  29.        public int compareTo(Object obj)
  30.        {
  31.                         
  32.             if(!(obj instanceof Student))//这里注意一下
  33.                 throw  new RuntimeException();
  34.          
  35.               Student s = (Student)obj;
  36.               int num = this.name.compareTo(s.name);
  37.               if(num==0)
  38.               {
  39.                     return new Integer(this.age).compareTo(new Integer(s.age));
  40.               }
  41.                         return num;
  42.         }                        
  43.                
  44. }
复制代码

//重新给你写了一个
作者: 净坛使者    时间: 2014-1-4 22:15
      if(obj instanceof Student){
                                throw  new RuntimeException();
                }
                Student s = (Student)obj;




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