一、接口的核心概念
1. 接口是什么?
-
行为契约:只定义方法签名(没有具体实现),规定“能做什么”,不关心“如何做”。
-
多继承的替代方案:一个类可以实现多个接口,弥补单继承的不足。
-
解耦利器:通过接口隔离调用方和实现方,提升代码扩展性和维护性。
2. 接口 vs 抽象类
| 特性 | 接口 | 抽象类 |
|---|---|---|
| 方法实现 | Java 8前只能有抽象方法,现支持默认方法 | 可以包含抽象方法和具体方法 |
| 变量类型 | 只能是public static final常量 | 可以是普通变量 |
| 继承机制 | 类可实现多个接口 | 类只能继承一个抽象类 |
| 设计目的 | 定义行为规范(如“可飞行”) | 抽取共性功能(如“动物”) |
二、接口的基础语法
1. 定义接口
-
使用
interface关键字,方法默认是public abstract。 -
可包含常量(默认
public static final)。
// 定义"可充电"接口
public interface Chargeable {
// 常量(电压)
int VOLTAGE = 220;
// 抽象方法(充电)
void charge();
// Java 8+ 默认方法(提供默认实现)
default void checkPower() {
System.out.println("检查电源是否正常");
}
// Java 8+ 静态方法
static void printVoltage() {
System.out.println("标准电压:" + VOLTAGE + "V");
}
}
2. 实现接口
-
类使用
implements关键字实现接口,必须重写所有抽象方法。 -
可同时实现多个接口(用逗号分隔)。
// 手机类实现Chargeable接口
public class Phone implements Chargeable {
private int battery;
@Override
public void charge() {
System.out.println("手机正在充电...");
battery += 50;
}
}
// 电动汽车类实现Chargeable和Drivable接口
public class ElectricCar implements Chargeable, Drivable {
@Override
public void charge() {
System.out.println("汽车快充中...");
}
@Override
public void drive() {
System.out.println("电能驱动行驶");
}
}
三、接口的高级特性
1. 默认方法(Default Method)
-
目的:在接口中添加新方法时,避免破坏已有实现类。
-
规则:使用
default修饰,提供默认实现,实现类可选择重写。
public interface USBDevice {
// 抽象方法
void transferData();
// 默认方法
default void plugIn() {
System.out.println("USB设备已连接");
}
}
// 实现类可不重写plugIn()
class Mouse implements USBDevice {
@Override
public void transferData() {
System.out.println("传输鼠标移动数据");
}
}
2. 静态方法(Static Method)
-
用途:提供与接口相关的工具方法,通过接口名直接调用。
public interface MathUtil {
static int max(int a, int b) {
return a > b ? a : b;
}
}
// 调用方式
int result = MathUtil.max(10, 20);
3. 接口继承(extends)
-
接口可以继承其他接口,支持多继承。
public interface Animal {
void eat();
}
public interface Flyable {
void fly();
}
// 接口多继承
public interface Bird extends Animal, Flyable {
void chirp();
}
// 实现类需实现所有方法
class Sparrow implements Bird {
@Override
public void eat() { /*...*/ }
@Override
public void fly() { /*...*/ }
@Override
public void chirp() { /*...*/ }
}
四、接口的实际应用场景
1. 回调机制(Callback)
-
通过接口实现事件监听。
// 定义点击监听接口
public interface OnClickListener {
void onClick();
}
// 按钮类
class Button {
private OnClickListener listener;
public void setListener(OnClickListener listener) {
this.listener = listener;
}
public void click() {
if (listener != null) {
listener.onClick();
}
}
}
// 使用:匿名内部类实现接口
Button btn = new Button();
btn.setListener(new OnClickListener() {
@Override
public void onClick() {
System.out.println("按钮被点击了!");
}
});
2. 策略模式(Strategy Pattern)
-
通过接口切换不同算法。
// 定义排序策略接口
public interface SortStrategy {
void sort(int[] array);
}
// 实现具体策略
class BubbleSort implements SortStrategy {
@Override
public void sort(int[] array) { /*冒泡排序实现*/ }
}
class QuickSort implements SortStrategy {
@Override
public void sort(int[] array) { /*快速排序实现*/ }
}
// 上下文类
class Sorter {
private SortStrategy strategy;
public void setStrategy(SortStrategy strategy) {
this.strategy = strategy;
}
public void executeSort(int[] array) {
strategy.sort(array);
}
}
五、常见问题解答
1. 什么时候使用接口?
-
需要定义跨继承体系的行为(如
Flyable可被飞机、鸟、超人实现)。 -
需要实现多重继承的效果。
-
定义模块间通信协议(如JDBC的
Connection接口)。
2. 接口可以实例化吗?
-
不能直接实例化,但可通过匿名内部类或Lambda表达式(函数式接口)创建实例。
Runnable task = new Runnable() {
@Override
public void run() {
System.out.println("任务执行中");
}
};
// Java 8+ Lambda表达式
Runnable task = () -> System.out.println("任务执行中");
3. 函数式接口(Functional Interface)
-
只有一个抽象方法的接口,可用Lambda表达式简化代码。
-
使用
@FunctionalInterface注解明确标识。
@FunctionalInterface
public interface Calculator {
int calculate(int a, int b);
}
// 使用Lambda实现
Calculator add = (a, b) -> a + b;
System.out.println(add.calculate(3, 5)); // 输出8


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



