java中comparable和comparator,类实现comparable接口
目录
1、比较器1.1比较接口1.2比较器接口2、可克隆接口2.1深拷贝和浅拷贝总结
1、比较器
比较器的引入
形容词(adjective的缩写)首先,当我们单一地比较某一种数据类型的数组时,可以直接用Arrays.sort()进行实现
b.而当我们同时含有多个参数时,并没有告诉我们按照什么来进行排序,此时,若是用Arrays.sort()就会出现报错的情况
基于这种情况,我们了解到,若是要将自定义类型进行大小比较,就要引入能够实现比较的接口,下面我们介绍Comparable和Comparator这两种比较器
1.1Comparable接口
实现Comparable接口的操作
通过Comparable接口实现年龄的排序
通过Comparable来实现名字的排序(注意名字是引用类,比较时应该是用compareTo()来进行)
升序降序
由于最终是利用的Arrays.sort()进行的比较,该方法底层是升序的操作,若是想转换为降序,只需要将重写的比较()方法中两项互换位置即可
变为降序后代码运行结果:
缺点!!!
可比较的对类的倾入性很强。由上面我们可知,要想比较新的类型就要更改比较()中的类型重新进行比较,这个在以后的工作中极大可能会使整个代码出现逻辑问题,可读性问题,因此我们引入下一类很灵活,倾入性不强的比较仪接口
整体代码如下:
导入Java。util。数组;班级学生实施比较公众年龄公共字符串名称;公开双分;公学生(int age,String name,double score){ this。年龄=年龄;this . name=name this . score=score } @ Override public String to String(){ return Student { age= age ,name= name ,score= score } } public static void main 3(String[]args){ Student Student 1=new Student(12,张三,98.0);学生学生2=新学生(18李四,97.9);//if(学生1。与(学生2)0相比)返回1;根据下面的方法进行进一步的返回系统。出去。println(学生1。与(学生2)相比);} public static void main(String[]args){ Student[]Student=new Student[3];学生[0]=新生(36,张三,98.0);学生[1]=新生(18,李四,97.9);学生[2]=新生(27,吴王,65.3);系统。出去。println(数组。tostring(学生));数组
.sort(student); System.out.println(Arrays.toString(student)); } public static void main1(String[] args) { int []array=new int []{2,5,3,6,8}; System.out.println(Arrays.toString(array)); Arrays.sort(array); System.out.println(Arrays.toString(array)); } @Override//谁调用这个方法,谁就是this public int compareTo(Student o) { //return this.age-o.age; return o.name.compareTo(this.name); }}
1.2Comparator接口
①实现Comparable接口的操作:
②通过该接口实现的姓名的比较:
③升序降序
执行后的结果:
④优点
灵活,对类的倾入性不强
⑤整体代码如下:
import java.util.Arrays;import java.util.Comparator;class Student { public int age; public String name; public double score; public Student(int age, String name, double score) { this.age = age; this.name = name; this.score = score; } @Override public String toString() { return "Student{" + "age=" + age + ", name=" + name + + ", score=" + score + }; }}class AgeComparator implements Comparator<Student> { @Override public int compare(Student o1, Student o2) { return o1.age-o2.age; }}class ScoreComparator implements Comparator<Student> { @Override public int compare(Student o1, Student o2) { return (int)(o1.score-o2.score); }}class NameComparator implements Comparator<Student> { @Override public int compare(Student o1, Student o2) { return o1.name.compareTo(o2.name); }}public class Test { public static void main2(String[] args) { Student students1 = new Student(1,"bit",98.9); Student students2 = new Student(2,"abc",88.9); /* if(students1.compareTo( students2) > 0) { }*/ //System.out.println(students1.compareTo( students2)); AgeComparator ageComparator = new AgeComparator(); System.out.println(ageComparator.compare(students1,students2)); } public static void main(String[] args) { Student[] student = new Student[3]; student[0] = new Student(12,"lisi",98.9); student[1] = new Student(6,"zangwu",88.9); student[2] = new Student(18,"whangsan",18.9); System.out.println(Arrays.toString(student)); AgeComparator ageComparator = new AgeComparator(); ScoreComparator scoreComparator = new ScoreComparator(); NameComparator nameComparator = new NameComparator(); Arrays.sort(student,nameComparator);//默认是从小到大的排序 System.out.println(Arrays.toString(student)); } public static void main1(String[] args) { int[] array = {1,21,3,14,5,16}; System.out.println(Arrays.toString(array)); Arrays.sort(array); System.out.println(Arrays.toString(array)); }}
2、Cloneable接口
①如何实现Cloneable接口:
Object 类中存在一个 clone 方法, 调用这个方法可以创建一个对象的 "拷贝". 但是要想合法调用 clone 方法, 必须要先实现 Clonable 接口, 否则就会抛出 CloneNotSupportedException
异常。
a.实现Cloneable接口
b.重写Cloneable方法
c.抛异常,强制类型转换
②面试中常问问题:
你知道Cloneable接口吗?为什么它是一个空接口,它有什么作用呢?
空接口,标志接口,代表这个类是可以被克隆的
③克隆的原理图:
④整体代码的实现:
class Person implements Cloneable{ public int age; public void eat(){ System.out.println("吃!"); } @Override public String toString() { return "Person{" + "age=" + age + }; } @Override protected Object clone() throws CloneNotSupportedException { return super.clone(); }}public class TestDemo { public static void main(String[] args) throws CloneNotSupportedException { Person person = new Person(); person.age=13; Person person2=(Person)person.clone(); System.out.println(person2); System.out.println(person); System.out.println("==========="); person2.age=14; System.out.println(person); System.out.println(person2); }}
2.1深拷贝和浅拷贝
①深浅拷贝:
决定是深拷贝还是浅拷贝,并不是方法的用途,而是代码的实现
②浅拷贝示例
浅拷贝代码如下:
class Money implements Cloneable{ public double m = 12.5;}class Person implements Cloneable{ public int age; public Money money = new Money(); public void eat() { System.out.println("吃!"); } @Override public String toString() { return "Person{" + "age=" + age + }; } @Override protected Object clone() throws CloneNotSupportedException { Person tmp = (Person)super.clone(); return tmp; }}public class TestDemo { public static void main(String[] args) throws CloneNotSupportedException { Person person = new Person(); Person person2 = (Person)person.clone(); System.out.println(person.money.m); System.out.println(person2.money.m); System.out.println("====================="); person2.money.m = 98.5; System.out.println(person.money.m); System.out.println(person2.money.m); }}
③深拷贝示例:(将tmp中的money也进行拷贝)
深拷贝代码如下:
class Money implements Cloneable{ public double m = 12.5; @Override protected Object clone() throws CloneNotSupportedException { return super.clone(); }}class Person implements Cloneable{ public int age; public Money money = new Money(); public void eat() { System.out.println("吃!"); } @Override public String toString() { return "Person{" + "age=" + age + }; } @Override protected Object clone() throws CloneNotSupportedException { Person tmp = (Person)super.clone(); tmp.money = (Money) this.money.clone(); return tmp; //return super.clone(); }}public class TestDemo { public static void main(String[] args) throws CloneNotSupportedException { Person person = new Person(); Person person2 = (Person)person.clone(); System.out.println(person.money.m); System.out.println(person2.money.m); System.out.println("====================="); person2.money.m = 98.5; System.out.println(person.money.m); System.out.println(person2.money.m); }}
总结
本篇文章就到这里了,希望能够给你带来帮助,也希望您能够多多关注盛行IT的更多内容!
郑重声明:本文由网友发布,不代表盛行IT的观点,版权归原作者所有,仅为传播更多信息之目的,如有侵权请联系,我们将第一时间修改或删除,多谢。