写了一个学生系统的程序,这是其中的一段代码,为了不浪费大家时间只是传了一段测试程序上来,帮忙看一下红色部分又上面问题啊??import java.util.TreeSet;;public class as implements Comparable{

public void main(String[] agrs){

TreeSet<Student> treeset = new TreeSet<Student>();
    Student s1=new Student(12345,"John","Smith");
Student s2=new Student(23456,"Jane","Smith");
Student s3=new Student(34567,"Adam","Smith");
Student s4=new Student(45678,"Tom","Jones");
treeset.add(s1);
treeset.add(s2);
treeset.add(s3);
treeset.add(s4);
for(Student student:treeset){
System.out.println(student.getFullname());
}
}

//实现Comparable接口的方法
public int compareTo(Object o){
assert((o instanceof Student)&&(o!=null));
Student s=(Student)o;
int relativeValue=LastName.comparaTo(s.getLastName());
if(relativeValue==0){
relativeValue=FirstName.comparaTo(s.getFirstName());
}

return relativeValue;

}}//这是student类
public class Student {
private long ID;
String FirstName=new String();
    String LastName=new String();
    public Student(long id,String fn,String ln){
     ID=id;
     FirstName=fn;
     LastName=ln;
     }
    public String getFirstName(){
     return FirstName;
    }
    public String getLastName(){
     return LastName;
    }
    public String getFullname(){
     return FirstName+" "+LastName;
    }
}

解决方案 »

  1.   


    int relativeValue=LastName.comparaTo(s.getLastName()); 
    if(relativeValue==0){ 
    relativeValue=FirstName.comparaTo(s.getFirstName()); 

    这里的LastName和FirstName是从哪里来的???
      

  2.   

    student类里面的啊 ??不是这么用的么?那该这么用啊?
      

  3.   

    晕哦,用也不是这么直接用吧,LastName和FirstName是Student类的私有属性啊,不能直接访问到的。
      

  4.   

    我知道,可是我在测试的时候也这么做了啊student.LastName还是不行啊???
      

  5.   

    你是想用TreeSet里的和参数比吧?
    那就从TreeSet里取个Student对象用get方法得到name
      

  6.   

    int relativeValue=this.LastName.comparaTo(s.getLastName()); 
    if(relativeValue==0){ 
    relativeValue=this.FirstName.comparaTo(s.getFirstName()); 
      

  7.   

    LastName.comparaTo(s.getLastName()); 
    compareTo()方法的名字写错了。。
      

  8.   

    LastName.comparaTo(s.getLastName()); 
    compareTo()方法的名字写错了。。