标签:extends col end com .so ati ons 怎么 java
需要对象是可比较的。
import java.util.*; public class Test { public static void main(String[] args) { ArrayList<Fruit> list = new ArrayList<>(); list.add(new Fruit(4)); list.add(new Fruit(1)); list.add(new Fruit(7)); Collections.sort(list); System.out.println(list); } } class Apple extends Fruit{ } class Fruit implements Comparable<Fruit>{ public int x; public int compareTo(Fruit f) { // return f.x - x; return x - f.x; } public Fruit(int x) { this.x = x; } public Fruit(){ x = 0; } @Override public String toString() { return "fruit: " + x; } }
思考几个问题?
标签:extends col end com .so ati ons 怎么 java
原文地址:https://www.cnblogs.com/hiyashinsu/p/10620470.html