-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathComparableTest.java
62 lines (56 loc) · 1.43 KB
/
ComparableTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package java_core_basic;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;
/**
* 实现Comparable接口完成对象之间的比较
* @author 唐龙
*/
public class ComparableTest {
public static void main(String[] args) {
Random r = new Random();
List<Hero2> heros = new ArrayList<Hero2>();
//通过随机值实例化hero的hp和damage
for (int i=0;i<10;i++) {
heros.add(new Hero2("hero-"+ i,r.nextInt(100),r.nextInt(100)));
}
System.out.println("初始化后的集合:\n"+heros);
//Hero2类实现了接口Comparable,即自带比较信息。
//Collections直接进行排序,无需额外的Comparator
Collections.sort(heros);
System.out.println("按照伤害低高排序后的集合:\n"+heros);
}
}
/**
* Hero2类
* 拥有姓名,血量,伤害度三个属性
* 实现接口Comparable<>
* 重载compareTo方法,按照demage属性从低到高排序
* @author 唐龙
*/
class Hero2 implements Comparable<Hero2>{
public String name;
public float hp;
public int damage;
public Hero2(String name) {this.name =name;}
//初始化name,hp,damage的构造方法
public Hero2(String name,float hp, int damage) {
this.name =name;
this.hp = hp;
this.damage = damage;
}
@Override
public int compareTo(Hero2 anotherHero) {
if(damage>=anotherHero.damage) {
return 1;
} else {
return -1;
}
}
//重写toString方法
@Override
public String toString(){
return "name="+name+" hp="+hp+" damage="+damage+"\n";
}
}