Java 接口(Interface)详细教学 1. 什么是接口(Interface)? 接口(Interface) 是 Java 中的一种特殊类型,它定义了一组方法但不提供实现 ,供类来实现(implement) 。接口用于定义行为规范,支持多态和解耦 ,增强代码的灵活性。
接口的特点:
只定义方法,不提供具体实现 (Java 8 及以上可以有默认方法)。
所有方法默认是 public abstract
(可以省略)。
接口中的变量默认是 public static final
(常量) 。
支持多实现 (一个类可以实现多个接口)。
不能实例化 ,只能被实现。
2. 如何定义接口? 接口的语法 1 2 3 4 5 6 7 interface 接口名 { 类型 变量名 = 值; 返回类型 方法名(参数列表); }
示例:定义一个接口 1 2 3 4 5 6 7 interface Animal { int EYES = 2 ; void eat () ; 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 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 之后,接口支持:
默认方法(default methods) :有具体实现的方法,子类可以直接使用或重写。
静态方法(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); } }
输出:
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 () ; } 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 () ; } interface C extends A , B { void methodC () ; } 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 语言的重要特性,掌握它可以让你的代码更加模块化、可扩展和灵活!🚀