接口的基本概念
接口由interface關鍵字定義
[接口修飾符] interface 接口名 [extends 基接口清單]{
接口體
}
注解
- [ ]表示可選部分
- 接口名一般以able為字尾(Cloneable),以表明能支援某種功能;或以字母I開頭(IMyInterface),以表明是一個接口
- 接口體可為空
接口的成員
- 接口的成員變量預設為 public static final
- 接口的方法預設為 public abstract
- 都不能帶除預設方法以外的任何修飾符
接口的繼承
接口可以被實作或者繼承多個。
接口的實作
接口可以由類來實作,派生類使用關鍵字implements實作接口。
interface C extends A,B{} :可以不實作A,B接口的方法。
class D implements A,B :必須實作A,B接口的方法。
interface IMyInterface1 {
public static final int AGE = 10;
public abstract void fun1();//不能有具體的實作
}
interface IMyInterface2 {
int AGE = 100;
void fun2();
}
interface IMyInterface3 extends IMyInterface1,IMyInterface2{
void fun3();
}
class Fairy implements IMyInterface1,IMyInterface2{
@Override
public void fun1() {
System.out.println("IMyInterface1");
}
@Override
public void fun2() {
System.out.println("IMyInterface2");
}
}
接口和抽象類的差別
- 接口内的方法必須不能被實作,而抽象類可以有部分非抽象方法.
- 抽象類隻能繼承一次,但是接口可以被實作或者繼承多個。
- 一個抽象類可以繼承一個抽象父類,但是一個接口可以使用關鍵字extends繼承多個接口。
- 抽象類是對類整體的抽象 而接口是對行為進行抽象。
- 在接口中的成員變量和成員方法預設為public static final和public abstract;抽象類當中的方法和成員變量沒用明确要求,但是抽象類當中的方法不能是private。
小練習
實作報警門
abstract class Door {
abstract void open();
abstract void close();
}
interface Alarm {
void alarm();
}
class AlarmDoor extends Door implements Alarm {
@Override
void open() {
}
@Override
void close() {
}
@Override
public void alarm() {
}
}
常用的接口
Cloneable接口
如果要克隆自定義類必須實作Cloneable接口,然後重寫Object類的克隆方法。

敲重點!!!
源代碼中接口内什麼都沒有,叫做空接口或标記接口。
空接口設計的作用:
标記目前類可以進行克隆,如果不實作這個接口,JVM不能夠識别。
class Money implements Cloneable{
double money = 10.0;
@Override
protected Object clone() throws CloneNotSupportedException {
return super.clone();
}
}
class Person implements Cloneable{
private String name;
Money m;
public Person(String name){
this.name = name;
this.m = new Money();
}
@Override//重寫Object類的clone()方法
protected Object clone() throws CloneNotSupportedException {
//淺拷貝
//return super.clone();
//深拷貝
Person person = (Person)super.clone();
person.m = (Money)this.m.clone();
return person;
}
}
public class Demo10 {
public static void main(String[] args)throws CloneNotSupportedException {
Person person = new Person("小可愛");
Person person1 = (Person) person.clone();
System.out.println(person.m.money);
System.out.println(person1.m.money);
System.out.println("===================");
person.m.money = 100.0;
System.out.println(person.m.money);
System.out.println(person1.m.money);
}
}
//運作結果
10.0
10.0
===================
100.0
10.0
Comparable接口
//源碼定義
public interface Comparable<T> {
public int compareTo(T o);
}
package com.tulun.src;
import java.util.Arrays;
class Student implements Comparable<Student>{
private String name;
private int age;
private double score;
public Student(String name,int age,double score){
this.name = name;
this.age = age;
this.score = score;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public double getScore() {
return score;
}
public void setScore(double score) {
this.score = score;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
", score=" + score +
'}';
}
@Override //重寫方法
public int compareTo(Student o) {
//return name.compareTo(o.name); 根據名字排序
return age-o.age; //根據年齡排序
}
}
public class Demo8{
public static void main(String[] args) {
Student[] students = new Student[3];
students[0] = new Student("zhang",10,99.0);
students[1] = new Student("li",20,59.0);
students[2] = new Student("an",30,19.0);
Arrays.sort(students);
System.out.println(Arrays.toString(students));
}
}
//運作結果
[Student{name='zhang', age=10, score=99.0}, Student{name='li', age=20, score=59.0}, Student{name='an', age=30, score=19.0}]
Comparator接口
//源碼
public interface Comparator<T> {
int compare(T o1, T o2);
}
package com.tulun.src;
import java.util.Arrays;
import java.util.Comparator;
class Student{
private String name;
private int age;
private double score;
public Student(String name,int age,double score){
this.name = name;
this.age = age;
this.score = score;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public double getScore() {
return score;
}
public void setScore(double score) {
this.score = score;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
", score=" + score +
'}';
}
}
public class Demo8{
public static void main(String[] args) {
Student[] students = new Student[3];
students[0] = new Student("zhang",10,99.0);
students[1] = new Student("li",20,59.0);
students[2] = new Student("an",30,19.0);
Arrays.sort(students, new Comparator<Student>() {
@Override
public int compare(Student o1, Student o2) {
//return o1.getAge()-o2.getAge(); //比較年齡
//return o1.getName().compareTo(o2.getName()); //比較名字
return (int)(o1.getScore()-o2.getScore()); //比較分數,需轉為int類型
}
});
System.out.println(Arrays.toString(students));
}
}
敲重點!!!
Comparable和Comparator的差別:
Comparable:類内部進行比較
Comparator:類外進行比較