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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© Lex 中级黑马   /  2014-6-4 21:51  /  1243 人查看  /  4 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

  1. import java.util.*;

  2. public class fanxing {

  3.         public static void main(String[] args) {
  4.                 // TODO Auto-generated method stub
  5.                 ArrayList<Person> a1 = new ArrayList<Person>();
  6.                 a1.add(new Person("abc1"));
  7.                 a1.add(new Person("abc2"));
  8.                 a1.add(new Person("abc3"));
  9.                
  10.                 ArrayList<Student> al1 = new ArrayList<Student>();
  11.                 al1.add(new Student("abc--1"));
  12.                 al1.add(new Student("abc--2"));
  13.                 al1.add(new Student("abc--3"));
  14.                 printColl(al1);
  15.         }
  16.         public static void printColl(ArrayList<? extends Person> a1){
  17.                 Iterator<? extends Person> it = a1.iterator();
  18.                 while(it.hasNext()){
  19.                         System.out.println(it.next().getName());
  20.                 }
  21.         }
  22. }

  23. class Person{
  24.         private String name;
  25.         Person(String name){
  26.                 this.name = name;
  27.         }
  28.         public String getName(){
  29.                 return name;
  30.         }
  31. }



  32. class Student   extends Person implements Comparable<Person>{
  33.         Student(String name){
  34.                 super(name);
  35.         }
  36.         public int compareTo(Person s){
  37.                 this.getName();
  38.                
  39.         }
  40. }

  41. class Comp implements Comparator<Person>{
  42.         public int compare(Student s1,Student s2){
  43.                 return s1.getName().compareTo(s2.getName());
  44.         }
  45. }
复制代码

4 个回复

倒序浏览
你的compareTo()方法都没有返回值
回复 使用道具 举报
楼主你的compareTo()方法都没有返回值,还有compare()方法要改成
public int compare(Person p1,Person p2){
                return p1.getName().compareTo(p2.getName());
}
回复 使用道具 举报
楼主不喜欢写注释。。虽然这些代码很简单
回复 使用道具 举报
一眼看上去好晕,半个注释都没。    错误:第42行的int compareTo(Person s)方法没有返回值
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马