package I_Template.b;
import java.util.Arrays;
/**
* 用模板方法排序
*/
public class Main {
public static void main(String[] args) {
Duck ducks[] = { new Duck(“1”, 1), new Duck(“2”, 4), new Duck(“3”, 7),
new Duck(“4”, 2) };
Arrays.sort(ducks);
System.out.println(Arrays.toString(ducks));
}
}
package I_Template.b;
public class Duck implements Comparable {
String name;
int weight;
public Duck(String name, int weight) {
this.name = name;
this.weight = weight;
}
public String toString() {
return "Duck [name=" + name + ", weight=" + weight + "]";
}
public int compareTo(Duck o) {
return this.weight - o.weight;
}
}