接口2
1. 接口间的继承
Java中,一个类于类是单继承,一个类可以是实现多个接口,接口于接口之间可以多继承。
接口继承接口使用extends关键字
狗这个接口,继承了跑接口和游泳接口
interface IRunning{
void Running();
}
interface ISwimming{
void Swimming();
}
interface Dog extends IRunning,ISwimming{
}
class Cat implements Dog{
}
2.重要接口
2.1运用Comparable接口
- 我们知道对一个数组进行排序的方法如下所示;
public class TestDemo2 {
public static void main(String[] args) {
int[] array={23,14,5,46,11};
Arrays.sort(array);//对数组排序
System.out.println("排序后"+Arrays.toString(array));//打印排序后的数组
}
}
- 那么现在有一个学生类,定义了姓名、年龄、分数,用上述方法对该类进行排序,会发现报错。
因为系统不知道你要进行排序的标准是什么,是年龄还是分数呢?
-
怎么进行修改呢?此时要让Student 类实现Comparable接口,并且实现其中的compareTo方法
-
比较当前对象和参数对象的大小关系:
-如果当前对象应排在参数对象之前,返回-1;
-如果当前对象应排在参数对象之后,返回1;
-如果当前对象和参数对象部分先后,返回0;代码如下:
if(this.age > o.age){//谁调用compareTo谁就是this
return 1;
}else if(this.age == o.age){
return 0;
}else{
return -1;
}
- 其中可用return this.age-o.age; 来对年龄进行排序;
- return (int) (this.score-o.score); 来对分数进行排序,因为分数是double类型所以需要将减下来的值强转为int;
- return this.name.compareTo(o.name); 对名字进行排序;(名字是String类型,不能减,要调用compareTo来进行比较)
class Student implements Comparable<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() {//toString方法为了打印学生类中的每个属性
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
", score=" + score +
'}';
}
@Override
public int compareTo(Student o) {
/*if(this.age > o.age){//谁调用compareTo谁就是this
return 1;
}else if(this.age == o.age){
return 0;
}else{
return -1;
}*/
//return this.age-o.age;
//return (int) (this.score-o.score);//需要将减下来的值强转为int
return this.name.compareTo(o.name);
}
}
public class TestDemo2 {
public static void main(String[] args) {
Student[] students=new Student[3];//定义了一个学生数组
students[0] =new Student("张三",19,78.6);
students[1] =new Student("李四",18,93.4);
students[2] =new Student("小王",24,88.8);
Arrays.sort(students);
System.out.println("排序后"+Arrays.toString(students));
}
}
2.2Comparator接口(比较器)
此时,要想也对分数进行排序,就要对代码进行修改。下面介绍一种可以直接添加对分数进行排序的方法。
class AgeComparator implements Comparator<Student>
如上述代码:让AgeComparator 类实现Comparator接口,并且实现其中的compare方法(也叫比较器)
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() {//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 ScoreComparator implements Comparator<Student>{
@Override
public int compare(Student o1, Student o2) {
return (int)(o1.score -o2.score);
}
}
public class TestDemo2 {
public static void main(String[] args) {
Student[] students=new Student[3];//定义了一个学生数组
students[0] =new Student("张三",19,78.6);
students[1] =new Student("李四",18,93.4);
students[2] =new Student("小王",24,88.8);
AgeComparator ageComparator=new AgeComparator();
System.out.println("排序前"+Arrays.toString(students));
Arrays.sort(students,ageComparator);
System.out.println("排序后"+Arrays.toString(students));
ScoreComparator scoreComparator=new ScoreComparator();
System.out.println("排序前"+Arrays.toString(students));
Arrays.sort(students,scoreComparator);
System.out.println("排序后"+Arrays.toString(students));
}
}
打印结果如下:
3.用接口实现拷贝
Object类中存在一个clone方法,调用这个方法可以创建一个对象的拷贝。想要合法的调用clone方法,必选实现一个Clonable接口。
再鼠标右键,点击Generate,在出现页面点击Override Methods,选择clone():Object,就可以在实现以下方法。
@Override
protected Object clone() throws CloneNotSupportedException {
return super.clone();
}
下列语句即可实现克隆
Person person2=(Person)person.clone();
class Person implements Cloneable{//克隆接口
public int id;
public Person(int id) {
this.id = id;
}
@Override
public String toString() {
return "Person{" +
"id=" + id +
'}';
}
@Override
protected Object clone() throws CloneNotSupportedException {
return super.clone();
}
}
public class TestDemo3 {
public static void main(String[] args) throws CloneNotSupportedException {
Person person = new Person(111);
System.out.println(person);
//把父类给子类,是向下转型,需要把person.clone()强转成Person
Person person2=(Person)person.clone();
System.out.println(person2);
System.out.println("修改一下ID");
person2 .id=112;
System.out.println(person.id);
System.out.println(person2.id);
}
}