首页 > 编程语言 > 详细

01.java8入门

时间:2019-09-09 17:55:21      阅读:96      评论:0      收藏:0      [点我收藏+]

函数式编程的不变模式


import java.util.Arrays;

/**
 * 函数式编程的不变模式
 */
public class ArrStream {
    public static void main(String[] args){
        int[] arr = {1,2,3,4,5};
        Arrays.stream(arr).map((x)->x+1).forEach(System.out::print);
        System.out.println();
        Arrays.stream(arr).forEach(System.out::print);
    }
    //23456
    //12345
}

FunctionalInterface注解

/**
 * FunctionalInterface注解
 */
@FunctionalInterface //用于表明是一个函数式接口
public  interface IntHandler {
    void handle(int i);//只包含一个抽象方法
}
@FunctionalInterface //用于表明是一个函数式接口
public  interface IntHandler {
    boolean equals(Object obj);//此时编译未通过,不是函数式接口,因为equals()方法在java.lang.Object中已经实现
}
/**
 * 符合函数式接口要求
 */
@FunctionalInterface //用于表明是一个函数式接口
public  interface IntHandler {
    boolean equals(Object obj);
    void handle(int i);
}

接口的默认方式--default关键字

public interface IHorse {
    void eat();
    //使用default关键字,可以在接口内定义实例方法
    default void run(){
        System.out.println("Horse run");
    }
}
public interface IAnimal {
    default void breath(){
        System.out.println("breath");
    }
}
public interface IDonkey {
    void eat();
    default void run(){
        System.out.println("Donkey run");
    }
}

/**
 * 同时拥有不同接口的实现方法
 */
public class Mule implements IAnimal,IHorse,IDonkey {
    @Override
    public void eat() {
        System.out.println("Mule eat");
    }
    @Override
    public void run(){
        IHorse.super.run();
    }
    public static void main(String[] args){
        Mule mule = new Mule();
        mule.run();
        mule.breath();
    }
    //Horse run
    //breath
}

01.java8入门

原文:https://www.cnblogs.com/fly-book/p/11492471.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!