java
中接口作用和生活中的接口类似,它提供一种约定,使实现接口的类在形式上保持一致。
抽象类中可以有普通方法二接口中的方法默认都是抽象的,也可以说接口是一个特殊的
抽象类
,接口不能被实例化,而且没有构造方法。
public interface 接口名 {
// 接口成员
}
语法解析
interface
修饰符。抽象类使用 abstract
修饰符。public
或包访问权限,与类的访问权限类似。[public] [static] [final] 数据类型 成员变量名 = 常量
;即 接口中成员变量默认都是 public、static、final 的,因此 public static final 可以省略。[public] [abstract] 返回值类型 方法名称(参数列表)
;即 接口中的方法默认都是 public、abstract 的,因此 public abstract 可以省略。与抽象类一样,使用接口要通过子类,子类通过关键字 implements
关键字实现接口。实现接口的语法格式如下:
public 类名 implements 接口名 {
// 实现方法
// 普通方法
// 属性
}
语法解析
implements
,实现抽象类使用关键字 extends
。public
权限,否则会产生编译错误。示例
/**
* 定义接口,关键词:interface
*/
public interface USBInterface { // 定义USB接口
void service();
}
// 定义U盘类,实现USB接口
class UDisk implements USBInterface {
// 实现接口的抽象方法
public void service() {
System.out.println("连接USB接口,开始传输数据");
}
}
// 定义风扇类,实现USB接口
class UsbFan implements USBInterface {
// 实现接口的抽象方法
public void service() {
System.out.println("连接USB接口,风扇开始转动");
}
}
public class UsbTest {
public static void main(String[] args) {
// U盘插入USB接口
USBInterface uDisk = new UDisk();
uDisk.service(); // 连接USB接口,开始传输数据
USBInterface usbFan = new UsbFan();
usbFan.service(); // 连接USB接口,风扇开始转动
}
}
接口本身也可以继承接口,接口继承的语法格式如下:
[修饰符] interface 接口名 extends 父接口1,父接口2,... {
// 常量定义
// 方法定义
}
java
中继承是单继承,使用 extends
关键字;但是一个类可以实现多个接口,使用 implements
, 多个接口之间用 ,
隔开。实现多个接口的语法如下:
class 类名 extends 父类名 implements 接口1,接口2,... {
// 类的成员
}
一个类可以同时继承和实现接口,extends
要在 implements
之前
public class LenovoComputer extends Computer implements USBInterface,ChargeInterface{
}
接口与接口之间是继承关系
,使用 extends
关键字。多个接口使用 ,
隔开。
public interface USBInterface {
void transferData();
}
public interface ChargeInterface {
void chargeDevice();
}
public interface USBC extends USBInterface, ChargeInterface {
// 可以添加自己的方法
}
public class Main {
public static void main(String[] args) {
USBC usbC = new SomeClass();
usbC.transferData(); // 调用继承自 USBInterface 的方法
usbC.chargeDevice(); // 调用继承自 ChargeInterface 的方法
}
}
class SomeClass implements USBC {
@Override
public void transferData() {
System.out.println("Transferring data via USB-C");
}
@Override
public void chargeDevice() {
System.out.println("Charging device via USB-C");
}
}
在 jdk8.0
中 default
关键字可用于在接口中修饰方法(默认方法),default
修饰的方法可以具体实现,也只能在接口中出现。default
修饰的方法可以被重写。
示例
public interface MyInterface {
void existingMethod();
default void newDefaultMethod() {
System.out.println("This is a new default method.");
}
}
public class MyClass implements MyInterface {
@Override
public void existingMethod() {
System.out.println("Existing method implementation.");
}
}
public class Main {
public static void main(String[] args) {
MyClass myClass = new MyClass();
myClass.existingMethod(); // 输出:Existing method implementation.
myClass.newDefaultMethod(); // 输出:This is a new default method.
}
}
默认方法可以在不破坏已经在使用该接口的所有代码。默认方法有时也称为防御方法(defender method)或 虚拟扩展方法(virtual extension method)
接口中还可以有 static
修饰的方法,称为静态方法(类方法)。 static
方法必须直接使用接口名.方法名调用。
public interface MyInterface {
void instanceMethod();
static void staticMethod() {
System.out.println("This is a static method.");
}
}
public class MyClass implements MyInterface {
@Override
public void instanceMethod() {
System.out.println("Instance method implementation.");
}
}
public class Main {
public static void main(String[] args) {
MyInterface.staticMethod(); // 输出:This is a static method.
MyClass myClass = new MyClass();
myClass.instanceMethod(); // 输出:Instance method implementation.
}
}
通过将接口中的方法声明为 private
,我们可以定义仅在接口内部使用的辅助方法,这些方法不会成为对外暴露的 API 的一部分。私有方法可以被接口中的其他方法调用,从而提供了代码复用和封装的能力。
public interface MyInterface {
void publicMethod();
default void defaultMethod() {
privateMethod();
System.out.println("This is a default method.");
}
private void privateMethod() {
System.out.println("This is a private method.");
}
}
public class MyClass implements MyInterface {
@Override
public void publicMethod() {
System.out.println("Public method implementation.");
}
}
public class Main {
public static void main(String[] args) {
MyClass myClass = new MyClass();
myClass.publicMethod(); // 输出:Public method implementation.
myClass.defaultMethod(); // 输出:This is a private method.
// This is a default method.
}
}
特性 | 接口 | 抽象类 |
---|---|---|
多继承 | 接口可以继承多个父类接口 | 子类只能继承一个直接抽象类 |
实现 | 子类通过implements 实现多个接口 |
子类通过extends 继承抽象类 |
成员 | 接口中只能有常量、抽象方法。JDK8.0及以后的版本中可以有static 方法和default 方法 |
抽象类中可以有实例成员、静态成员抽象方法。抽象类中不能使用default 关键字修饰 |
成员修饰符 | 接口中只能定义常量(public static final 修饰的变量) | 抽象类可以定义变量,也可以定义常量 |
子类实现 | 子类在实现抽象方法时必须指定public 权限 |
子类在实现抽象方法时不影响缩小访问权限 |
构造函数 | 接口中不能定义构造函数 | 抽象类可以有构造函数,但不能进行实例化 |
最高层 | 接口没有最高层 | 类的最高层是Object 类 |