持续创作,加速成长!这是我参与「掘金日新计划 · 10 月更文挑战」的第20天
一、接口实现实例
我们对Person类型数组进行排序.
class Person {
public String name;
public int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
public static void main(String[] args) {
Person[] person = {new Person("张三",18),new Person("lisi",20),new Person("wangwu",21)};
Arrays.sort(person);
System.out.println(Arrays.toString(person));
}
我们这里对Person数组进行排序,根据的是name,还是age?
在系统的排序的方法中会将数组类型强制转换为Comparable,但我们的Person未实现Comparable接口,所以会报类转换异常.
那让母后我们来让Person类实现Comparable功能.
我们在实现Comparable功能时,系统提示我们必须实现compareTo方法.
@Override
public int compareTo(Person o) {
if(this.age > o.age) {
return 1;
}else if(this.age < o.age) {
return -1;
}else {
return 0;
}
}
现在我们试着排序一些Person数组
我们可以发现Person数组已经能够根据age进行排序了.
这时候有同学就要问了:
这里为什么要这样书写,其实这个的Person是一个泛型,但大家不用去研究它,我们来看一下String的源码.
我们可以发现也是这样写的,我们只需要模仿就行了.
大家想一下要是不想按age排序了,现在想实现按照name排序,那么我们该怎么去写?
直接去修改CompareTo方法吗?这里CompareTo已经被其他程序调用过了,我们直接修改会造成程序紊乱,我们可以这样写:
class NameComparator implements Comparator<Person> {
@Override
public int compare(Person o1, Person o2) {
return o1.name.compareTo(o2.name);
}
}
public static void main(String[] args) {
Person[] person = {new Person("zhangsan",18),new Person("lisi",20),new Person("wangwu",21)};
NameComparator nameComparator = new NameComparator();
Arrays.sort(person,nameComparator);
System.out.println(Arrays.toString(person));
}
我们可以写一个类去实现Comparator接口去指定比较对象的内容.
我们这里自己写一个冒泡排序,去按照Person的age去排序
public static void BubbleSort(Comparable[] arr) {
for (int i = 0; i < arr.length-1; i++) {
for (int j = 0; j < arr.length-1-i; j++) {
if(arr[j].compareTo(arr[j+1]) > 0) {
Comparable tmp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = tmp;
}
}
}
}
public static void main(String[] args) {
Person[] person = {new Person("zhangsan",18),new Person("lisi",20),new Person("wangwu",21)};
BubbleSort(person);
System.out.println(Arrays.toString(person));
}
二、Clonable实现深浅拷贝
当我们想实现对象的拷贝时,我们就需要实现Clonable接口.
//定义一个学生类
class Student implements Cloneable {
public String name;
}
我们可以发现Clonable接口是一个空接口,也可以称作标记接口.
我们发现Student类实现了Clonable接口但还是不能克隆.
我们在底层发现,clone()方法是protected修饰的,不同包只能通过子类super.调用,所以我们在Student必须重写这个方法.
class Student implements Cloneable {
public String name;
@Override
protected Object clone() throws CloneNotSupportedException {
return super.clone();
}
}
我们重写之后发现,还是不能调用clone()方法,我们继续看底层代码.
我们可以发现父类抛了一个异常,那么我们也必须加上.
因为它返回的是一个Object类型的,我们必须强制转换为Student类型.
public static void main(String[] args) throws CloneNotSupportedException {
Student student = new Student();
student.name = "张三";
Student student1 = (Student) student.clone();
}
public static void main(String[] args) throws CloneNotSupportedException {
Student student = new Student();
student.name = "张三";
Student student1 = (Student) student.clone();
System.out.println(student);
System.out.println(student1);
}
我们打印了一下对象,这时候Student对象已经克隆成功,但这里只是浅拷贝.
class IScore {
double score;
}
class Student implements Cloneable {
public String name;
public IScore iScore = new IScore();
@Override
protected Object clone() throws CloneNotSupportedException {
return super.clone();
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
'}';
}
}
public static void main(String[] args) throws CloneNotSupportedException {
Student student = new Student();
student.name = "张三";
Student student1 = (Student) student.clone();
student.iScore.score = 100;
System.out.println("student: "+student.iScore.score);
System.out.println("student1: "+student1.iScore.score);
}
我们可以发现在克隆后,修改student的score,student1的score也被修改了.
我们发现,两个对象指向了同一块Score,所以我们要将Score也进行克隆.
class IScore implements Cloneable{
double score;
@Override
protected Object clone() throws CloneNotSupportedException {
return super.clone();
}
}
class Student implements Cloneable {
public String name;
public IScore iScore = new IScore();
@Override
protected Object clone() throws CloneNotSupportedException {
Student student = (Student)super.clone();
student.iScore = (IScore)this.iScore.clone();
return student;
}
}
这样就可以实现深拷贝.
三、抽象类与接口的区别
1、抽象类中可以包含普通方法,但接口中只能包含public与abstract方法(JDK 1.8之前),JDK1.8之后允许接口中出现default方法; 2、抽象类中的成员变量没有访问权限的限制,但接口中的变量只能被public static final修饰; 3、一个接口可以继承多个接口,但一个类只能有一个父类,类可以实现多个接口; 4、抽象类是对一类事物的抽象,接口则是对行为的抽象。一个类继承一个抽象类代表“是不是”的关系,而一个类实现一个接口则表示“有没有”的关系。 核心区别: 抽象类中可以包含普通方法和普通字段, 这样的普通方法和字段可以被子类直接使用(不必重写), 而接口中不能包含普通方法, 子类必须重写所有的抽象方法.