package com.sf.javase.day16; public class User implements Comparable{ private int id; private String name; public User(int id, String name) { this.id = id; this.name = name; } @Override public String toString() { return "User{" + "id=" + id + ", name='" + name + '\'' + '}'; } @Override public int compareTo(Object o) { if(o instanceof User){ User user = (User) o; return this.id - user.id; }else { throw new RuntimeException("类型异常"); } } }