Java 接口(Interface)详细教学

1. 什么是接口(Interface)?

接口(Interface) 是 Java 中的一种特殊类型,它定义了一组方法但不提供实现,供类来实现(implement)。接口用于定义行为规范,支持多态和解耦,增强代码的灵活性。

接口的特点:

  1. 只定义方法,不提供具体实现(Java 8 及以上可以有默认方法)。
  2. 所有方法默认是 public abstract(可以省略)。
  3. 接口中的变量默认是 public static final(常量)
  4. 支持多实现(一个类可以实现多个接口)。
  5. 不能实例化,只能被实现。

2. 如何定义接口?

接口的语法

1
2
3
4
5
6
7
interface 接口名 {
// 常量(默认 public static final)
类型 变量名 = 值;

// 抽象方法(默认 public abstract)
返回类型 方法名(参数列表);
}

示例:定义一个接口

1
2
3
4
5
6
7
// 定义一个 Animal 接口
interface Animal {
int EYES = 2; // 默认 public static final

void eat(); // 默认 public abstract
void sleep();
}

3. 如何实现接口?

implements 关键字

  • 使用 implements 关键字,类必须实现接口中的所有方法,否则类必须声明为 abstract
  • 一个类可以实现多个接口

示例:实现接口

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
// 定义 Dog 类,实现 Animal 接口
class Dog implements Animal {
@Override
public void eat() {
System.out.println("Dog is eating...");
}

@Override
public void sleep() {
System.out.println("Dog is sleeping...");
}
}

// 测试
public class TestInterface {
public static void main(String[] args) {
Dog dog = new Dog();
dog.eat();
dog.sleep();
System.out.println("Dog has " + Dog.EYES + " eyes."); // 访问接口常量
}
}

运行结果:

1
2
3
Dog is eating...
Dog is sleeping...
Dog has 2 eyes.

4. 接口的默认方法和静态方法(Java 8+)

Java 8 之后,接口支持:

  1. 默认方法(default methods):有具体实现的方法,子类可以直接使用或重写。
  2. 静态方法(static methods):只能通过接口名调用,不能被子类重写。

示例:默认方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
interface Animal {
void eat();

// 默认方法
default void sleep() {
System.out.println("Animal is sleeping...");
}
}

class Cat implements Animal {
@Override
public void eat() {
System.out.println("Cat is eating...");
}
}

public class TestDefaultMethod {
public static void main(String[] args) {
Cat cat = new Cat();
cat.eat();
cat.sleep(); // 直接调用接口的默认方法
}
}

输出:

1
2
Cat is eating...
Animal is sleeping...

示例:静态方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
interface MathUtil {
// 静态方法
static int add(int a, int b) {
return a + b;
}
}

// 直接调用接口的静态方法
public class TestStaticMethod {
public static void main(String[] args) {
int sum = MathUtil.add(10, 20);
System.out.println("Sum: " + sum);
}
}

输出:

1
Sum: 30

5. 接口的多实现

一个类可以实现多个接口,这在 Java 不支持多继承 的情况下非常有用。

示例:一个类实现多个接口

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
interface Animal {
void eat();
}

interface Swimmer {
void swim();
}

// Frog 同时实现两个接口
class Frog implements Animal, Swimmer {
@Override
public void eat() {
System.out.println("Frog is eating...");
}

@Override
public void swim() {
System.out.println("Frog is swimming...");
}
}

public class TestMultipleInterfaces {
public static void main(String[] args) {
Frog frog = new Frog();
frog.eat();
frog.swim();
}
}

输出:

1
2
Frog is eating...
Frog is swimming...

6. 接口的继承

接口也可以继承接口,一个接口可以继承多个接口。

示例:接口继承接口

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
interface A {
void methodA();
}

interface B {
void methodB();
}

// C 继承 A 和 B
interface C extends A, B {
void methodC();
}

// 实现 C 的类必须实现 A 和 B 的方法
class MyClass implements C {
@Override
public void methodA() {
System.out.println("Method A");
}

@Override
public void methodB() {
System.out.println("Method B");
}

@Override
public void methodC() {
System.out.println("Method C");
}
}

public class TestInterfaceInheritance {
public static void main(String[] args) {
MyClass obj = new MyClass();
obj.methodA();
obj.methodB();
obj.methodC();
}
}

输出:

1
2
3
Method A
Method B
Method C

7. instanceof 判断接口实现

如果一个对象实现了某个接口,我们可以使用 instanceof 进行判断。

示例

1
2
3
4
5
6
7
8
9
10
11
12
13
public class TestInstanceOf {
public static void main(String[] args) {
Frog frog = new Frog();

if (frog instanceof Animal) {
System.out.println("Frog is an Animal.");
}

if (frog instanceof Swimmer) {
System.out.println("Frog is a Swimmer.");
}
}
}

输出:

1
2
Frog is an Animal.
Frog is a Swimmer.

8. 抽象类 vs. 接口

特性 抽象类 接口
是否可以有方法实现 可以 不能(Java 8+ 支持默认方法)
是否可以有构造方法 可以 不能
是否可以有成员变量 可以 只能有 public static final 常量
是否支持多继承 不能(单继承) 可以(多实现)
适用场景 具有共性行为的类(”是什么”) 定义行为规范(”能做什么”)

9. 总结

  • 接口用于定义一组行为规范,类可以 implements(实现)接口
  • 接口中的方法默认是 public abstract,变量是 public static final
  • Java 8+ 支持 default 方法(有默认实现)和 static 方法
  • 一个类可以实现多个接口,接口可以继承多个接口
  • 使用 instanceof 可以判断对象是否实现了某个接口
  • 接口提供了解耦和灵活性,适用于多态和规范约束

接口是 Java 语言的重要特性,掌握它可以让你的代码更加模块化、可扩展和灵活!🚀