class Student implements Comparable<Student> {
private int score;
public Student(int score) {
this.score = score;
}
public int getScore() {
return score;
}
@Override
public int compareTo(Student o) {
return Integer.compare(this.score, o.score);
}
}
public class GenericMethodExample {
public static <T extends Comparable<T>> T max(T a, T b) {
return a.compareTo(b) >= 0? a : b;
}
public static void main(String[] args) {
Student student1 = new Student(85);
Student student2 = new Student(92);
Student result = max(student1, student2);
System.out.println("成绩较高的学生成绩: " + result.getScore());
}
}