接口
接口的定义
接口是一种特殊的抽象类,它不能被实例化,只能被实现。在 Java 编程里,接口是构建代码结构的重要工具,它定义了一组方法的签名,却不包含具体的实现。接口有助于达成多态性、提高代码的可维护性与可扩展性。
接口中可以包含抽象方法和非抽象方法。从 Java 8 开始,接口支持默认方法(default method)与静态方法(static method),这些都属于非抽象方法。
接口中的方法默认都是抽象方法,默认都是 public
,默认都是 public abstract
。这意味着在接口里定义方法时,无需显式地写上 public
和 abstract
关键字。
接口中的变量默认都是 public static final
。这表明接口中的变量是常量,一旦赋值就不能再改变。
示例
[public] interface 接口名 [extends 父接口名列表]{
[public] [final] [static] 类型 常量名=常量值;
[public] [abstract] 返回类型 方法名(参数列表);
}
public interface ChineseEmployee {
String nationality="Chinese"; //public static final
double pay(); //abstract
}
以上示例定义了一个名为 ChineseEmployee
的接口,该接口包含一个常量 nationality
以及一个抽象方法 pay()
。
接口的实现
类对接口予以实现。为了声明一个类要实现接口,在类的声明中要包括一条 implements
语句。一个类可以实现多个接口,因此可以在 implements
后面可列出类要实现的接口系列,这些接口以逗号分隔。
示例
public class Employee implements ChineseEmployee {
public double pay() {
return 1000;
}
}
在这个例子中,Employee
类实现了 ChineseEmployee
接口,并且实现了 pay()
方法。
接口的继承
一个接口可以继承多个接口,因此可以在 extends
后面可列出接口系列,这些接口以逗号分隔。接口继承能够让新接口继承父接口的方法和常量,进而拓展功能。
示例
interface Employee {
String getName(); //abstract
}
interface ChineseEmployee extends Employee {
String nationality="Chinese"; //public static final
}
interface WomanEmployee extends Employee {
String gender="Woman"; //public static final
String getName(); //abstract
}
class WomanChineseEmployee implements ChineseEmployee, WomanEmployee {
private String name;
public WomanChineseEmployee(String name) {
this.name = name;
}
public String getName() {
return nationality + " " + gender + " " + name;
}
}
在上述示例中,ChineseEmployee
接口和 WomanEmployee
接口都继承自 Employee
接口。WomanChineseEmployee
类实现了这两个接口,并且实现了 getName()
方法。
接口的实现
一个类可以实现多个接口,因此可以在 implements
后面可列出类要实现的接口系列,这些接口以逗号分隔。实现多个接口能让类具备多个接口的特性,增强类的功能。
更多示例
// 定义一个可移动的接口
interface Movable {
void move();
}
// 定义一个可攻击的接口
interface Attackable {
void attack();
}
// 实现多个接口的类
class Warrior implements Movable, Attackable {
public void move() {
System.out.println("Warrior is moving.");
}
public void attack() {
System.out.println("Warrior is attacking.");
}
}
public class Main {
public static void main(String[] args) {
Warrior warrior = new Warrior();
warrior.move();
warrior.attack();
}
}
在这个示例中,Warrior
类实现了 Movable
和 Attackable
两个接口,并且实现了这两个接口中的方法。在 Main
类的 main
方法中,创建了 Warrior
类的对象,并调用了 move()
和 attack()
方法。
接口的默认方法和静态方法
从 Java 8 开始,接口支持默认方法和静态方法。默认方法用 default
关键字修饰,为接口方法提供默认实现;静态方法用 static
关键字修饰,可直接通过接口名调用。
示例
// 定义一个包含默认方法和静态方法的接口
interface Shape {
double area();
// 默认方法
default void draw() {
System.out.println("Drawing a shape.");
}
// 静态方法
static void printInfo() {
System.out.println("This is a shape interface.");
}
}
// 实现接口的类
class Circle implements Shape {
private double radius;
public Circle(double radius) {
this.radius = radius;
}
public double area() {
return Math.PI * radius * radius;
}
}
public class ShapeMain {
public static void main(String[] args) {
Circle circle = new Circle(5);
circle.draw();
System.out.println("Area of the circle: " + circle.area());
Shape.printInfo();
}
}
在这个示例中,Shape
接口包含一个抽象方法 area()
、一个默认方法 draw()
和一个静态方法 printInfo()
。Circle
类实现了 Shape
接口,并实现了 area()
方法。在 ShapeMain
类的 main
方法中,创建了 Circle
类的对象,调用了 draw()
和 area()
方法,还通过接口名调用了静态方法 printInfo()
。