本次引入两个接口使用实例。
给定一个学生类:
class Student{
private String name;
private int age;
private int score;
public Student(String name,int age,int score){
this.name = name;
this.age = age;
this.score = score;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
", score=" + score +
'}';
}
}
在给定一个学生数组,我们需要将这个对象数组当中的元素进行排序(分别按照名字、年龄、分数)。
Student[] students = new Student[]{
new Student("小明",15,60),
new Student("小红",12,100),
new Student("小华",16,85),
};
对于数组来说可以直接使用sort 方法就可以直接进行比较了,但是对于现在的自定义类型来说,自定义类型进行排序是需要实现Comparable接口的,并实现其中的compareTo抽象方法。
class Student implemnents Comparable<Student>{
private String name;
private int age;
private int score;
public Student(String name,int age,int score){
this.name = name;
this.age = age;
this.score = score;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
", score=" + score +
'}';
}
public int compareTo(Student o){
return this.name.compareTo(o.name);
//return this.age - o.age; 年龄比较
//return this.score - o.score;分数比较
}
}
public class JavaTest01{
public static void main(String[] args) {
Student[] students = new Student[]{
new Student("小明",15,60),
new Student("小红",12,100),
new Student("小华",16,85),
};
System.out.println(Arrays.toString(students));
Arrays.sort(students);
System.out.println(Arrays.toString(students));
}
}
当然也可以重写 sort 方法(使用冒泡排序):
public static void sort(Comparable[] comparables){
for (int i = 0;i < comparables.length;i++){
for (int j =0 ;j < comparables.length - 1 - i;j++){
if(comparables[j].compareTo(comparables[j+1]) > 0){
Comparable temp = comparables[j];
comparables[j] = comparables[j+1];
comparables[j+1] = temp;
}
}
}
}
在数组当中就有array.clone(),就能够克隆原数组然后返回新数组,这样的拷贝是深拷贝,即修改原数组并不会影响到新数组的值。
现如今,要将一个人的有关属性全部拷贝完成。给定一个人的类和一个拥有金钱的类,如果要进行拷贝就需要使用clone()方法:
要使用clone()方法,首先需要实现Cloneable接口。
此处便会产生一个疑问:既然Cloneable接口没有抽象方法,为什么还要实现Cloneable呢?这里的空接口又被称之为标记接口,表示当前的接口可以被克隆,需要重写克隆方法。
上图就是需要重写的clone()方法,其中必须对
java.lang.CloneNotSupportedException进行捕获或声明以便抛出。那么完整代码如下所示:
class Person implements Cloneable{
public String name;
Money m;
public Person(String name){
this.name = name;
this.m =new Money();
}
@Override
protected Object clone() throws CloneNotSupportedException {
return super.clone();
}
}
class Money{
double money = 12.9;
}
public class JavaTest02{
public static void main(String[] args)throws CloneNotSupportedException {
Person person = new Person("小红");
Person person1 = (Person) person.clone();
System.out.println(person.m.money+","+person1.m.money);
person1.m.money = 100;
System.out.println(person.m.money+","+person1.m.money);
}
}
----------------------------------------------
输出结果:
12.9,12.9
100.0,100.0
通过输出结果我们不难看出,这个过程只是进行了浅拷贝。只是因为对于Money对象只是拷贝了它的引用,导致改变了原对象的数值也影响到了新数组的数值。
因此需要将Money类的引用一同发生改变,即再创建一个Money对象。
代码如下:
class Person implements Cloneable{
public String name;
Money m;
public Person(String name){
this.name = name;
this.m =new Money();
}
@Override
protected Object clone() throws CloneNotSupportedException {
Person p = (Person)super.clone();//第一步
p.m = (Money)this.m.clone();//第二步
return p;
}
}
class Money implements Cloneable{
double money = 12.9;
@Override
protected Object clone() throws CloneNotSupportedException {
return super.clone();
}
}
public class JavaTest02{
public static void main(String[] args)throws CloneNotSupportedException {
Person person = new Person("小红");
Person person1 = (Person) person.clone();//第三步
System.out.println(person.m.money+","+person1.m.money);
person1.m.money = 100;
System.out.println(person.m.money+","+person1.m.money);
}
}
-----------------------------
输出结果:
12.9,12.9
12.9,100.0