class Student
{
private String name;
private int age;
Student(String name,int age)
{
this.name = name;
this.age = age;
}
int compareTo_1(Student s)
{
return this.age-s.age; //age不是私有字段吗,为什么s的age字段能在这里直接访问呀,私有字段不是只能在类内部进行访问吗,比如在getAge()方法中可以访问。 }
String getName()
{
return name;
}
int getAge()
{
return age;
}
public static void main(String[] args)
{
Student s1 = new Student("zhangsan",19);
Student s2 = new Student("lisi",21);
System.out.println(s1.compareTo_1(s2));
}
} |