- class Person implements Comparable<Person>
- {
- private String name;
- Person(String name)
- {
- this.name = name;
- }
- public int compareTo(Person p)
- {
- return this.name.compareTo(p.name);
- }
- }
- class Test
- {
- public static void main(String[] args)
- {
- new Person("1").compareTo(new Person("2"));
- //System.out.println(new Person("3").name);
- }
- }
复制代码 为什么compareTo方法可以访问p的私有成员name |