ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

JDK8接口新特性

2021-09-17 20:06:11  阅读:182  来源: 互联网

标签:default void FunctionalInterface 特性 JDK8 接口 interface public


JDK8接口新特性

JDK8中对接口规范进行了新的定义,允许在接口中定义默认方法(使用default关键字修饰),静态方法,同时还推出了函数式接口(使用@FunctionInterface注解描述)设计。

default方法设计及实现

package com.cnblogs.newStu;

public interface TestInter {

    default void show(){
        System.out.println("default!!!");
    }

    default void say(){
        System.out.println("hello!!!");
    }

    void stu();
}

class TestInterImpl implements TestInter{
    public static void main(String[] args) {
        TestInterImpl testInter = new TestInterImpl();
        testInter.show();
        testInter.say();
        testInter.stu();
    }

    //必须要重写
    @Override
    public void stu() {
        System.out.println("我");
    }
}
/*
default!!!
hello!!!
我
*/

static方法设计及实现

package com.cnblogs.newStu;

public interface TestInter2 {
    static void show(){
        System.out.println("我来了!!!");
    }
}

class study{
    public static void main(String[] args) {
        TestInter2.show();
    }
}

//我来了!!!

函数式接口设计及实现

Java8引入了一个是函数式接口(Functional Interfaces),此接口使用
@FunctionalInterface修饰,并且此接口内部只能包含一个抽象方法。

package com.cnblogs.newStu;

@FunctionalInterface
public interface TestInter3 {
    void show();
}

函数式接口推出的目的主要是为了配合后续Lambda表达式的应用。

应用案例增强分析及实现

消费型接口

@FunctionalInterface
public interface Consumer<T> {
    void accept(T t);
}

函数型接口

@FunctionalInterface
public interface Function<T, R> {
    R apply(T t);
}

判定型接口

@FunctionalInterface
public interface Predicate {
boolean test(T t);
}

供给型接口

@FunctionalInterface
public interface Supplier<T> {
    T get();
}

标签:default,void,FunctionalInterface,特性,JDK8,接口,interface,public
来源: https://www.cnblogs.com/fangweicheng666/p/15306070.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有