import java.util.Arrays;
class BookCook implements Comparable<BookCook>{
private String title;
private double price;
public BookCook(String title,double price){
this.title = title;
this.price = price;
}
@Override
public String toString() {
return "书名:"+this.title+",价格:"+this.price;
}
@Override
public int compareTo(BookCook o) {
if(this.price > o.price){
return 1;
}else if(this.price < o.price){
return -1;
}else{
return 0;
}
}
}
class Student {
private String name;
private double score;
public Student(String name,double score){
this.name = name;
this.score = score;
}
public double getScore(){
return this.score;
}
@Override
public String toString() {
return "姓名:"+this.name+",分数:"+this.score;
}
}
class StudentComparator implements Comparator<Student> {
@Override
public int compare(Student o1,Student o2) {
if(o1.getScore() > o2.getScore()){
return 1;
}else if(o1.getScore() < o2.getScore()){
return -1;
}else{
return 0;
}
}
}
public class TestComparator {
public static void main(String[] args) {
Student[] sts = new Student[]{
new Student("小戴",60),
new Student("小王",90),
new Student("老王",80),
new Student("小萱",95)
};
java.util.Arrays.sort(sts, new StudentComparator());
System.out.println(java.util.Arrays.toString(sts));
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有