1234567891011121314151617181920212223242526272829 |
- 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("类型异常");
- }
- }
- }
|