面向对象——多态,抽象类,接口(二)-1
https://developer.aliyun.com/article/1504154
接口常用实例
4.1 comparable接口
在java中,我们在数组中就学过了Arrays.sort()对一个整数类型的数组进行排序,那么对于在实际处理业务,有些时候排序需要我们指定一个指标进行排序,这个时候就要用到我们java中自带的comparable接口
class Student implements Comparable<Student>{ public String name; public int age; public double score; //这里的this是谁调用了compareTo方法谁就是 @Override public int compareTo(Student o) { //return this.age-o.age;//通过年龄进行比较 //return (int)(this.score-o.score);//通过分数进行比较 return this.name.compareTo(o.name);//甚至可以通过名字进行排序 } public Student(String name, int age, double score) { this.name = name; this.age = age; this.score = score; } @Override public String toString() { return "Student{" + "name='" + name + '\'' + ", age=" + age + ", score=" + score + '}'; } } public class demo { public static void main(String[] args) { Student student=new Student("haha",24,55); Student student1=new Student("heihei",20,89); System.out.println(student.compareTo(student1)); } }
我们甚至可以利用这个接口对更多个数组排序,实现这个接口之后,利用Arrays.sort()进行排序
public static void main(String[] args) { Student[] students =new Student[3]; students[0]=new Student("zhan",21,98); students[1]=new Student("hehe",17,87); students[2]=new Student("keke",34,99); Arrays.sort(students); System.out.println(Arrays.toString(students)); }
这个类存在一定的缺点,对于类的侵害性大,所以下面我们在介绍comparator接口
4.2 comparator接口
先创立一个学生类
class Student { public String name; public int age; public double score; public Student(String name, int age, double score) { this.name = name; this.age = age; this.score = score; } @Override public String toString() { return "Student{" + "name='" + name + '\'' + ", age=" + age + ", score=" + score + '}'; } }
创立一个年龄比较器
class AgeComparator implements Comparator<Student>{ @Override public int compare(Student o1, Student o2) { return o1.age-o2.age; } }
创立一个分数比较器
//成绩比较器 class ScoreComrarator 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 demo { public static void main1(String[] args) { Student student=new Student("haha",24,55); Student student1=new Student("heihei",20,89); ScoreComrarator scoreComrarator=new ScoreComrarator(); scoreComrarator.compare(student1,student); } }
比较多个数组的一个成绩
public class demo { public static void main(String[] args) { Student[] students =new Student[3]; students[0]=new Student("zhan",21,98); students[1]=new Student("hehe",17,87); students[2]=new Student("keke",34,99); ScoreComrarator scoreComrarator=new ScoreComrarator(); Arrays.sort(students,scoreComrarator); System.out.println(Arrays.toString(students)); } }
4.3 clonable接口和深拷贝
4.3.1 浅拷贝
class people{ public int age; } public class apple { public static void main(String[] args) { people people=new people(); people people1=people; System.out.println(people); System.out.println(people1); } }
也就是所浅拷贝就是简单来理解就是会有两个引用公用了一个对象。
4.3.2 深拷贝与clonable接口
class people implements Cloneable { public int age; @Override protected Object clone() throws CloneNotSupportedException { return super.clone(); } } public class apple { public static void main(String[] args)throws CloneNotSupportedException { people people=new people(); people people1=(people) people.clone(); System.out.println(people); System.out.println(people1); } }
所谓的深拷贝简单理解就是一个引用对应单独一个对象
4.3.3 clonable使用要点:
1 clonable接口源码是不包含任何抽象方法的,是一个空接口,对于这个空接口的作用就是表示当前类是可以被克隆的。
2 对于clone方法结合clonable我们必须要在实现接口中重写方法。
3 由于clone源码是一个object类,所以这里我们必须进行类型强转。
4 实现clone接口中需要我们抛异常,这个在后续的学习中,我们会讲解异常。
浅拷贝与深拷贝的详细理解参考这篇文章: