import java.util.*; //導(dǎo)入Java包中的類
public class Main{
? ? public static void main(String[] args) {
? ? ? ? class NameComparator implements Comparator<PersonSortable2>{
? ? ? ? ? ? public int compare(PersonSortable2 n1, PersonSortable2 n2) {
? ? ? ? ? ? ? ? return n1.getName().compareTo(n2.getName());
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? class AgeComparator implements Comparator<PersonSortable2>{
? ? ? ? ? ? public int compare(PersonSortable2 a1, PersonSortable2 a2) {
? ? ? ? ? ? ? ? return a1.getAge()-a2.getAge();//這里不能使用compareTo方法
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? Scanner sc = new Scanner(System.in);
? ? ? ? int n = sc.nextInt();? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //對象數(shù)組
? ? ? ? PersonSortable2 t[] = new PersonSortable2 [n];
? ? ? ? for(int i = 0;i < n;i++)? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //i從0開始遞增
? ? ? ? {
? ? ? ? ? ? String a = sc.next();? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //一次性讀取一行
? ? ? ? ? ? int b = sc.nextInt();? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //一次性讀取一行
? ? ? ? ? ? PersonSortable2 person = new PersonSortable2(a, b);
? ? ? ? ? ? t[i] = person;
? ? ? ? }? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //按照姓名排序
? ? ? ? System.out.println("NameComparator:sort");
? ? ? ? Arrays.sort(t, new NameComparator()) ;
? ? ? ? for(PersonSortable2 i:t) {
? ? ? ? ? ? System.out.println(i);
? ? ? ? }? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //按照年齡排序
? ? ? ? System.out.println("AgeComparator:sort");
? ? ? ? Arrays.sort(t, new AgeComparator());
? ? ? ? for(PersonSortable2 i : t){
? ? ? ? ? ? System.out.println(i);
? ? ? ? }? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //輸出接口的類型
? ? ? ? System.out.println(Arrays.toString(NameComparator.class.getInterfaces()));
? ? ? ? System.out.println(Arrays.toString(AgeComparator.class.getInterfaces()));
? ? ? ? sc.close();
? ? }
}
class PersonSortable2 {
? ? private String name;? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //字符串name私有化
? ? private int age;? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //整型age私有化
? ? public PersonSortable2(String name, int age) {
? ? ? ? super();
? ? ? ? this.name = name;? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //調(diào)用name
? ? ? ? this.age = age;? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //調(diào)用age
? ? }
? ? @Override? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //重寫
? ? public String toString() {
? ? ? ? return name+"-"+age;
? ? }
? ? public String getName() {
? ? ? ? return name;
? ? }
? ? public void setName(String name) {
? ? ? ? this.name = name;? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //調(diào)用name
? ? }
? ? public int getAge() {
? ? ? ? return age;
? ? }
? ? public void setAge(int age) {
? ? ? ? this.age = age;? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //調(diào)用age
? ? }
}