阅读 70

JDK8接口新特性

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 {
    void accept(T t);
}

函数型接口

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

判定型接口

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

供给型接口

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

原文:https://www.cnblogs.com/fangweicheng666/p/15306070.html

文章分类
百科问答
版权声明:本站是系统测试站点,无实际运营。本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 XXXXXXo@163.com 举报,一经查实,本站将立刻删除。
相关推荐