Java类库中需要重写的⽅法
⼀:前⾔
1.Java常⽤的类库中,有⼏个⽅法是需要重写,在重写之后,才能根据我们的需求操作。
2.需要重写的类:toString()⽅法,equals()⽅法,ComparaTo()⽅法,Comparator()⽅法
⼆:开⼲:
1.重写toString()⽅法:
1.1)⼀个实体类要是想打印与该类有关的内容,必须重写他,可以系统⽣成,也可以根据⾃⼰的需要重写
1.2)代码演⽰:(这个超级简单)
1public class Student {
2private String name;
3private int age;
4
5public Student(){};
6
7public Student(String name, int age) {
8this.name = name;
9this.age = age;
10 }
11
12 @Override
13public String toString() {
14return"Student{" +
15"name='" + name + '\'' +
16", age=" + age +
17'}';
18 }
19 }
2.重写equals()⽅法:
2.1)先来看下equals()⽅法的底层源码
1public static boolean equals(Object a, Object b) {
2return (a == b) || (a != null && a.equals(b));
3 }
源码在底层也是⽤“==”来表⽰的。(在基本的数据类型中,==是⽤来判断内容是否相同的,在引⽤数
据类型中,==⽤来判断两个对象在堆内存中的地址是否相同,如果想要判断对象的内容是否相等,就要按照⾃⼰的需求重写该⽅法。
2.2)代码演⽰:
1public class Student {
2 String name;
3int age;
4
5public Student(){};
6
7public Student(String name, int age) {
8this.name = name;
9this.age = age;
10 }
11
12 @Override
13public boolean equals(Object o) {
14//⾸先⽐较两个的地址是否相同,如果相同,则⼀定是同⼀个对象
15if(this == o){java的tostring方法
16return true;
17 }
18//如果传进来的对象为空
19if(o==null){
20return false;
21 }
22//在判断传进来的对象的类型是否是本类类型或者是他的⼦类
23if(o instanceof Student){
24//将对象转换为Person类
25 Student student =(Student)o;
26if(student.name.equals(name) && student.age==age){
27return true;
28 }
29 }
30//创建来的不是学⽣对象,肯定也是false
31return false;
32 }
33
34 }
3.重写compareTo()⽅法
3.1)该⽅法是⽤在集合排序时⽤的,如果你想如果你想按照⾃⼰定的规则排序,就要从写该⽅法3.2)要实现重写compareTo()⽅法,该类就要实现Compareable接⼝
3.3)如果返回的为1,则表⽰当前对象⼤写,按照升序排序;为-1,为降序;为0则相同
3.4)代码演⽰:
学⽣类:
1public class Student implements Comparable<Student> {
2 String name;
3int age;
4
5public Student(){};
6
7public Student(String name, int age) {
8this.name = name;
9this.age = age;
10 }
11
12
13 @Override
14public int compareTo(Student o) {
15//现按年龄升序排序,年龄相同就按照名字字母排序
16if(this.age>o.age){
17return1;
18 }
19if(this.age<o.age){
20return -1;
21 }
22else{
23return this.namepareTo(o.name);
24 }
25 }
26
27 @Override
28public String toString() {
29return"Student{" +
30"name='" + name + '\'' +
31", age=" + age +
32'}';
33 }
34 }
主⽅法:
1 import java.util.ArrayList;
2 import java.util.Collections;
3 import java.util.Iterator;
4
5public class Test {
6public static void main(String[] args) {
7 ArrayList <Student> list = new ArrayList<>();
8 Student student1 = new Student("张海梦",12);
9 Student student2 = new Student("吴红霞",12);
10 list.add(student1);
11 list.add(student2);
12//先调⽤⽅法对集合排序
13 Collections.sort(list);
14 Iterator<Student> iterator = list.iterator();
15while(iterator.hasNext()){
16 Student next = ();
17 System.out.println(next);
18 }
19 }
20 }
4.重写Comparator()⽅法:
4.1)利⽤Comparator()⽅法,要新建⼀个类,该类继承Comparator接⼝,泛型为我们要⽐较的实现类4.2)实体类:
1public class Worker {
2private String name;
3private Integer age;
4
5public Worker() {
6 }
7
8public Worker(String name, Integer age) {
9this.name = name;
10this.age = age;
11 }
12
13public String getName() {
14return name;
15 }
16
17public void setName(String name) {
18this.name = name;
19 }
20
21public Integer getAge() {
22return age;
23 }
24
25public void setAge(Integer age) {
26this.age = age;
27 }
28
29 @Override
30public String toString() {
31return"Worker{" +
32"name='" + name + '\'' +
33", age=" + age +
34'}';
35 }
36 }
4.2)Comparator类
1 import java.util.Comparator;
2
3/**
4 * 编写⼀个类,实现Comparator接⼝,泛型为⼯⼈类
5*/
6public class WorkerComparator implements Comparator<Worker> { 7 @Override
8public int compare(Worker o1, Worker o2) {
Age()&Age()){
10return1;
11 }
Age()&Age()){
13return -1;
14 }
15else{
Name()Name());
17 }
18 }
19 }
4.3)测试类;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
public class Test01 {
public static void main(String[] args) {
//创建集合存储⼯⼈对象
ArrayList<Worker>list = new ArrayList<>();
list.add(new Worker("⼩梦",15));
list.add(new Worker("⼤⽶",15));
//创建⼀个comparator对象
WorkerComparator comparator = new WorkerComparator();
/
/调⽤⽅法对集合进⾏排序
// Collections.sort(集合,⽐较类);
Collections.sort(list,comparator);
Iterator<Worker> iterator = list.iterator();
while(iterator.hasNext()){
Worker next = ();
System.out.println(next);
}
}
}
三:总结:以上就是重写⽅法,加油!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论