当前位置:首页 > Java

java如何写接口

2026-03-02 22:16:07Java

如何在Java中编写接口

Java中的接口(Interface)是一种抽象类型,用于定义一组方法规范,供类实现。接口可以包含抽象方法、默认方法、静态方法和常量。

定义接口

使用interface关键字定义接口,接口中的方法默认是public abstract,变量默认是public static final

public interface MyInterface {
    // 抽象方法
    void abstractMethod();

    // 默认方法(Java 8+)
    default void defaultMethod() {
        System.out.println("This is a default method.");
    }

    // 静态方法(Java 8+)
    static void staticMethod() {
        System.out.println("This is a static method.");
    }

    // 常量
    String CONSTANT = "CONSTANT_VALUE";
}

实现接口

类使用implements关键字实现接口,必须实现接口中的所有抽象方法。

java如何写接口

public class MyClass implements MyInterface {
    @Override
    public void abstractMethod() {
        System.out.println("Implemented abstract method.");
    }
}

使用接口

通过接口引用调用方法,可以实现多态。

public class Main {
    public static void main(String[] args) {
        MyInterface obj = new MyClass();
        obj.abstractMethod(); // 调用实现的抽象方法
        obj.defaultMethod();  // 调用默认方法
        MyInterface.staticMethod(); // 调用静态方法
        System.out.println(MyInterface.CONSTANT); // 访问常量
    }
}

接口的多重实现

一个类可以实现多个接口,解决Java单继承的限制。

java如何写接口

public interface InterfaceA {
    void methodA();
}

public interface InterfaceB {
    void methodB();
}

public class MyClass implements InterfaceA, InterfaceB {
    @Override
    public void methodA() {
        System.out.println("Method A");
    }

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

接口继承

接口可以继承其他接口,形成接口的层次结构。

public interface ParentInterface {
    void parentMethod();
}

public interface ChildInterface extends ParentInterface {
    void childMethod();
}

public class MyClass implements ChildInterface {
    @Override
    public void parentMethod() {
        System.out.println("Parent method");
    }

    @Override
    public void childMethod() {
        System.out.println("Child method");
    }
}

函数式接口(Java 8+)

只有一个抽象方法的接口称为函数式接口,可以用@FunctionalInterface注解标记。

@FunctionalInterface
public interface MyFunctionalInterface {
    void execute();
}

public class Main {
    public static void main(String[] args) {
        // 使用Lambda表达式实现
        MyFunctionalInterface func = () -> System.out.println("Executing...");
        func.execute();
    }
}

标签: 如何写接口
分享给朋友:

相关文章

答辩vue接口如何实现

答辩vue接口如何实现

Vue 接口实现的核心方法 使用 Axios 或 Fetch 发起 HTTP 请求 Axios 是 Vue 项目中常用的 HTTP 客户端,需先通过 npm install axios 安装。在组件中…

React如何写popover

React如何写popover

使用React创建Popover React中实现Popover可以通过多种方式,包括使用第三方库或自定义组件。以下是几种常见方法: 使用Material-UI库 Material-UI提供了现成的…

react插件如何写

react插件如何写

React 插件开发基础 React 插件通常以 npm 包形式发布,核心是通过封装可复用的组件或逻辑供其他项目调用。需要遵循 React 组件设计规范,并考虑兼容性、性能优化和文档完整性。 项目初…

react如何写tabs路由

react如何写tabs路由

实现React中的Tabs路由 使用React Router结合Tabs组件可以创建导航式的标签页路由。以下是实现方法: 安装依赖包 确保项目已安装react-router-dom和必要的UI库(如…

react如何写小程序

react如何写小程序

React 开发小程序的方法 React 本身是为 Web 应用设计的,但可以通过以下方式开发小程序: 使用 Taro 框架 Taro 是一个多端统一开发框架,支持使用 React 语法开发小程序(…

react中如何写重置按钮

react中如何写重置按钮

重置按钮的实现方法 在React中,重置按钮通常用于将表单或组件的状态恢复到初始值。以下是几种常见的实现方式: 使用表单的reset方法 对于原生HTML表单元素,可以直接调用reset()方法:…