本帖最后由 zhangle 于 2014-5-5 12:45 编辑
- class Student implements Comparable//该接口强制让学生具备比较性
- {
- private String name;
- private int age;
- Student(String name,int age)
- {
- this.name = name;
- this.age = age;
- }
- public int compareTo(Object obj)
- {
- return 1;
- //return -1;
- /*
- if(!(obj instanceof Student))
- throw new RuntimeException("不是学生对象");
- Student s = (Student)obj;
- System.out.println(this.name+"...compare..."+s.name);
- if(this.age>s.age)
- return 1;
- if(this.age==s.age)//比较
- {
- return this.name.compareTo(s.name);
- }
- return -1;
- */
- }
- public void setName(String n)
- {
- name = n;
- }
- public void setAge(int a)
- {
- age = a;
- }
- public String getName()
- {
- return name;
- }
- public int getAge()
- {
- return age;
- }
- }
复制代码
为什么不能用 public void setName(String name)
{
this.name = name;
}
|
|