文章目录
- 接口
- 1.1 接口概述
- 1.2 接口的语法
- 1.3 接口的使用
- 1.4 多接口
- 1.5 接口中的比较接口
- 1.5.2 Comparable 接口
- 1.5.3 Comparable 接口语法
- 1.5.4 Comparable 接口的使用
- 1.5.5 Comparator接口
- 1.5.6 Comparator接口语法
- 1.5.7 Comparator接口的使用
- 1.5.8 Comparator接口中String类型的比较
- 1.6 Cloneable接口和深拷贝
- 1.6.2 Cloneable接口的语法
- 1.6.3 Cloneable接口的使用
- 1.6.3.2 调用克隆方法
接口
1.1 接口概述
在Java中不支持多继承,而接口可以。
接口生活中到处可见,如USB接口,电脑的电源接口等,只要符合UBS接口的要求就可以使用USB接口,由此接口就是公共的行为规范标准,大家在实现时,只要符合规范标准,就可以
通用。
在Java中,接口可以看成是:多个类的公共规范,是⼀种引⽤数据类型
1.2 接口的语法
public interface 接口名 {
}
和类的定义差不多,只是将class变成了interface
接口定义需要注意的几点:
- 接口名的定义以 I 为开头
- 接口名的定义使用形容词词性的单词来定义
接口中成员变量默认定义:public static final
接口中成员方法默认定义:public ababstract

默认定义可写可不写
注意: 接口不能被实例化

1.3 接口的使用
接口不能直接拿来使用,接口的使用,需要依附于类,必须要一个“实现类”实现接口,才能使用接口
要实现接口则需要使用 implements 关键字
子类和父类之间用的是extends关键字,而类和接口之间则是用 implements 来实现
类和接口之间的实现分为以下几步:
- 实现类和接口使用implements连接
- 重写接口当中的成员方法

public interface IRaced {
void run();
}
public class Test implements IRaced{
@Override
public void run() {
}
}
注意:
-
接口中被default修饰的方法,可以在接口中实现,可以不被重写,如果要重写这个方法也可以实现重写

-
在接口中,子类重写接口的方法,重写方法的访问修饰符一定要大于等于父类的访问修饰符

-
接口中不能有静态代码块和构造方法

1.4 多接口
Java中不支持多继承,但接口可以实现多接口,所以当类需要“多继承”时,可以使用多接口去实现
接口间的继承相当于把多个接口合并在一起
实现多接口的接口当中也可以定义方法

1.5 接口中的比较接口
public class Test {
public static void main(String[] args) {
int a = 2;
int b = 1;
if (a > b) {
System.out.println("a > b");
}
}
}
上述代码是常见的比较大小的方法,还有一种方法也是使用 引用 来比较大小
public class Test {
public static void main(String[] args) {
Student student1 = new Student("zhangsan",18,60);
Student student2 = new Student("lisi",18,59);
if (student1.scores > student2.scores) {
System.out.println("student1 > student2");
}
}
}
在Java中也有Java官方提供的比较大小的接口
1.5.2 Comparable 接口
1.5.3 Comparable 接口语法
public class 类名 implements Comparable<o> {
}
<>的 o 即代表存放要比较的类
注意:重写后的compareTo方法就不能更改
1.5.4 Comparable 接口的使用
分为两步:
- 在实现类中使用 implements 实现Comparable 接口
- 重写Comparable 接口中的compareTo方法

public class Student implements Comparable<Student>{
public String name;
public int age;
public double scores;
public Student(String name, int age, double scores) {
this.name = name;
this.age = age;
this.scores = scores;
}
@Override
public int compareTo(Student o) {
return this.age - o.age;
}
}
public class Test {
public static void main(String[] args) {
Student student1 = new Student("zhangsan",18,60);
Student student2 = new Student("lisi",17,59);
if (student1.compareTo(student2) > 0) {
System.out.println("student1 > student2");
}
}
}
1.5.5 Comparator接口
1.5.6 Comparator接口语法
public class 类名 implements Comparator<o> {
}
和Comparable 接口一样
- <>中存放要比较的类
- 重写compare方法
1.5.7 Comparator接口的使用
- 在实现类中使用 implements 实现Comparator接口
- 重写Comparator接口中的compare方法
和Comparable 接口不同的是,Comparator更灵活一些,可以根据不同的比较对象,来创建不同的类,进行不同的比较。
Comparable 一般是Java默认的比较方式
public class Student implements Comparator<Student> {
public String name;
public int age;
public double scores;
public Student(String name, int age, double scores) {
this.name = name;
this.age = age;
this.scores = scores;
}
@Override
public int compare(Student o1, Student o2) {
return o1.age - o2.age;
}
}
public class Test {
public static void main(String[] args) {
Student student1 = new Student("zhangsan",18,60);
Student student2 = new Student("lisi",17,59);
int set = student1.compare(student1, student2);
if (set > 0) {
System.out.println("student1 > student2");
}
}
}
上述提到Comparator接口,可以根据不同的比较对象,创建不同的类来进行比较,我们把这种类叫做比较器
public class ScoresComparator implements Comparator<Student> {
@Override
public int compare(Student o1, Student o2) {
return (int)(o1.scores - o2.scores);
}
}
注意:(int)(o1.scores - o2.scores)这里进行了强转,因为scores是double类型的,但重写的compare方法是int的类型,这里强转的int会导致double类型的变量精度丢失,所以这里的写法不推荐,也不要使用
1.5.8 Comparator接口中String类型的比较
compare方法不能比较String类型,String是引用类型,在Java中引用类型不能直接用大于小于等于比较运算符进行比较
而在String类下存在 Comparable接口,所以一定存在compareTo方法,虽然compare方法不能比较String类型,但可以通过compareTo方法进行比较
public 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 main(String[] args) {
Student student1 = new Student("zhangsan",18,60);
Student student2 = new Student("lisi",17,59);
NameComparator nameComparator = new NameComparator();
int set = nameComparator.compare(student1, student2);
if (set > 0) {
System.out.println("student1 > student2");
}
}
}
1.6 Cloneable接口和深拷贝
Cloneable接口是一个空接口,也叫标记接口
标记接口:如果一个类实现了这个接口,表示当前这个类可以被克隆
Cloneable接口分为浅拷贝和深拷贝

1.6.2 Cloneable接口的语法
public class Student implements Cloneable{
}
Cloneable接口特殊的它是一个空接口,想要实现克隆功能,必须要调用它的克隆方法,而克隆方法在Object类中
1.6.3 Cloneable接口的使用
使用Cloneable接口分为3步:
- 实现 Cloneable接口
- 调用克隆方法

1.6.3.2 调用克隆方法
快捷键:Ctrl + o 可以直接到第3步

浅拷贝:
public class Student implements Cloneable {
public String name;
public int age;
public double scores;
public Student(String name, int age, double scores) {
this.name = name;
this.age = age;
this.scores = scores;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
", scores=" + scores +
'}';
}
@Override
protected Object clone() throws CloneNotSupportedException {
return super.clone();
}
}
public class Test {
public static void main(String[] args) throws CloneNotSupportedException{
Student s1 = new Student("zhangsan", 18, 80);
Student s2 = (Student) s1.clone();
System.out.println(s1);
System.out.println(s2);
}
}
//结果:
//Student{name='zhangsan', age=18, scores=80.0}
//Student{name='zhangsan', age=18, scores=80.0}
下面这个例子可以更好理解浅拷贝
浅拷贝只拷贝当前对象的内容,而深拷贝,对象当中的对象都要完成克隆
class Money implements Cloneable{
double money = 9.9;
}
public class Student implements Cloneable {
public String name;
public int age;
public double scores;
public Money m = new Money();
public Student(String name, int age, double scores) {
this.name = name;
this.age = age;
this.scores = scores;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
", scores=" + scores +
'}';
}
@Override
protected Object clone() throws CloneNotSupportedException {
return super.clone();
}
}
public class Test {
public static void main(String[] args) throws CloneNotSupportedException{
Student s1 = new Student("zhangsan", 18, 80);
Student s2 = (Student) s1.clone();
System.out.println(s1.m.money);
System.out.println(s2.m.money);
System.out.println("====================");
s2.m.money = 19.9;
System.out.println(s1.m.money);
System.out.println(s2.m.money);
}
}
//结果
//9.9
//9.9
//====================
//19.9
//19.9
深拷贝
还是以上述例子为例进行深拷贝
class Money implements Cloneable{
double money = 9.9;
@Override
protected Object clone() throws CloneNotSupportedException {
return super.clone();
}
}
public class Student implements Cloneable {
public String name;
public int age;
public double scores;
public Money m = new Money();
public Student(String name, int age, double scores) {
this.name = name;
this.age = age;
this.scores = scores;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
", scores=" + scores +
'}';
}
@Override
protected Object clone() throws CloneNotSupportedException {
Student tmp = (Student) super.clone();
tmp.m = (Money) this.m.clone();
return tmp;
}
}
public class Test {
public static void main(String[] args) throws CloneNotSupportedException{
Student s1 = new Student("zhangsan", 18, 80);
Student s2 = (Student) s1.clone();
System.out.println(s1.m.money);
System.out.println(s2.m.money);
System.out.println("====================");
s2.m.money = 19.9;
System.out.println(s1.m.money);
System.out.println(s2.m.money);
}
}
//结果:
//9.9
//9.9
//====================
//9.9
//19.9
注意:
在Test类中的main方面后面还有一段代码 —>throws CloneNotSupportedException,多这一段代码的原因是因为,如果没有这一段代码会报运行时异常,关于异常这里暂且不表

而加上这一段代码则解决了编译时异常这个问题

9050

被折叠的 条评论
为什么被折叠?



