详细分析@FunctionalInterface的基本知识(附Demo)

前言

Java的基本知识推荐阅读:

  1. java框架 零基础从入门到精通的学习路线 附开源项目面经等(超全)
  2. Spring框架从入门到学精(全)

1. 基本知识

@FunctionalInterface 是 Java 8 引入的一个注解,用于标记一个接口为函数式接口

函数式接口是只包含一个抽象方法的接口,可以有多个默认方法或静态方法,通常用于 lambda 表达式和方法引用

  • 标记接口为函数式接口,明确接口的设计意图,使代码更易读,便于他人理解接口的用途
  • 编译器会确保被标记的接口只有一个抽象方法。如果接口有多于一个抽象方法,编译器会报错,从而避免接口被错误地使用

2. Demo

基本的使用法则如下:

定义函数式接口以及使用lambda

@FunctionalInterface
interface MyFunctionalInterface {
    void myMethod();
}


public class FunctionalInterfaceDemo {
    public static void main(String[] args) {
        MyFunctionalInterface func = () -> System.out.println("Hello, Functional Interface!");
        func.myMethod();
    }
}

多个默认方法和静态方法

@FunctionalInterface
interface MyFunctionalInterface {
    void myMethod();
    
    // 默认方法
    default void defaultMethod() {
        System.out.println("This is a default method.");
    }
    
    // 静态方法
    static void staticMethod() {
        System.out.println("This is a static method.");
    }
}


public class FunctionalInterfaceDemo {
    public static void main(String[] args) {
        MyFunctionalInterface func = () -> System.out.println("Hello, Functional Interface!");
        func.myMethod();
        func.defaultMethod();
        MyFunctionalInterface.staticMethod();
    }
}

为了进一步说明此注解的主要性:(编译检查)

加上注解:

在这里插入图片描述

给一个实际的Demo例子如下:

// 定义函数式接口
@FunctionalInterface
interface CorrectFunctionalInterface {
    void singleMethod();
    
    // 可以有默认方法
    default void defaultMethod() {
        System.out.println("This is a default method.");
    }
    
    // 可以有静态方法
    static void staticMethod() {
        System.out.println("This is a static method.");
    }
}


// 具体使用
public class FunctionalInterfaceDemo {
    public static void main(String[] args) {
        // 使用 lambda 表达式实现 singleMethod
        CorrectFunctionalInterface func = () -> System.out.println("Hello from singleMethod!");
        
        // 调用 singleMethod
        func.singleMethod();
        
        // 调用默认方法
        func.defaultMethod();
        
        // 调用静态方法
        CorrectFunctionalInterface.staticMethod();
    }
}

截图如下:

在这里插入图片描述

相关推荐

  1. 详细分析Vue3中卡槽知识点(Demo

    2024-07-10 08:44:03       30 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-10 08:44:03       99 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-10 08:44:03       107 阅读
  3. 在Django里面运行非项目文件

    2024-07-10 08:44:03       90 阅读
  4. Python语言-面向对象

    2024-07-10 08:44:03       98 阅读

热门阅读

  1. IT专业入门,高考假期预习指南

    2024-07-10 08:44:03       35 阅读
  2. 强化OT安全英国发布工控网络事件响应实践指南

    2024-07-10 08:44:03       41 阅读
  3. 使用静态图加速

    2024-07-10 08:44:03       23 阅读
  4. 修改ES索引名称

    2024-07-10 08:44:03       27 阅读
  5. asp.netWebForm(.netFramework) CSRF漏洞

    2024-07-10 08:44:03       38 阅读
  6. Redis的使用(三)常见使用场景-session共享

    2024-07-10 08:44:03       31 阅读
  7. DS200CVMAG1AEB处理器 控制器 模块

    2024-07-10 08:44:03       37 阅读
  8. 插8张显卡的服务器有哪些?

    2024-07-10 08:44:03       29 阅读
  9. react antd table拖拽

    2024-07-10 08:44:03       31 阅读
  10. VB 关键字

    2024-07-10 08:44:03       35 阅读
  11. 前端面试题(13)答案版

    2024-07-10 08:44:03       35 阅读
  12. 智能警卫:Conda包依赖的自动监控之道

    2024-07-10 08:44:03       34 阅读
  13. vue处理重复请求

    2024-07-10 08:44:03       29 阅读
  14. 深度学习:从数据采集到模型测试的全面指南

    2024-07-10 08:44:03       25 阅读