java 函数方法

本文章解释函数方法里面其中的三种

自定义返回值的函数方法

先定义一个方法 (Request, Response 实体在本文下方)

	// 可以看到有两个参数, 第一个参数是我们的入参, 第二个参数就是函数方法
	//(Function 是有泛型的,第一个对象是入参, 第二个对象是返回值)
	// 方法内可以做一些方法的前置处理
   	public Response fundDemo (Request request, Function<Request<String, String>, Response> function) {
        System.out.println("fundDemo 前置校验中...");
        if (request.getId() != null && request.getName() != null) {
        	// apply 方法是把 request 对象当成我们的入参, 返回值是 Response
            return function.apply(request);
        }
        return new Response("401", "前置校验失败", "");
    }

方法调用的时候如下:

// 首先是传入的两个参数, 第一个是 Request 对象, 第二个是函数是方法
// 语法是 () -> {}, () 内的值就是刚才 function.apply() 传入的 Request 对象
// {} 内是自己处理数据,以及定义返回值
		Response response = new FundDemo().fundDemo(new Request(null, "test", "x"), (request) -> {
            if (request.getId() == "1") {
                return new Response("200", "成功", "");
            } else {
                return new Response("500", "失败", "故意错误!");
            }
        });

无返回值函数方法

// 第一个参数还是入参,第二个参数是函数对象
	public void fundDemo (Request<String, String> request, BiConsumer<String ,String> function) {
		// accept 执行之前可处理数据,以及方法的逻辑, 然后传入参数
        function.accept(request.getId(), request.getName());
    }

方法调用的时候如下:

// 第一个参数为入参, 第二次参数是函数
// (id, name) 为 accept 方法传入的两个参数(参数为固定的,只能传两个)
// {} 内是处理逻辑,该方法无返回值
		new FundDemo().fundDemo(new Request("2", "test", "x"), (id, name) -> {
            if (id == "1") {
                System.out.println(new Response("200", "成功", ""));
            } else {
                System.out.println(new Response("500", "失败", "故意错误!"));
            }
        });

boolean 返回值函数方法

// request 是入参
// predicate 是函数对象,泛型是函数表达式中的值
	public boolean fundDemo1 (Request request, Predicate<String> predicate) {
		// test为执行函数
        return predicate.test(request.getId().toString());
    }

方法调用如下:

		boolean predicate = new FundDemo().fundDemo1(new Request("2", "test", "x"), (id) -> {
            if (id == "1") {
                return true;
            } else {
                return false;
            }
        });

实体类如下:

public class Request<id, name> {
    private id id;
    private name name;
    private String sex;

    public Request(id id, name name, String sex) {
        this.id = id;
        this.name = name;
        this.sex = sex;
    }

    public id getId() {
        return id;
    }

    public void setId(id id) {
        this.id = id;
    }

    public name getName() {
        return name;
    }

    public void setName(name name) {
        this.name = name;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }
}
public class Response {
    private String code;
    private String msg;
    private String data;

    @Override
    public String toString() {
        return "Response{" +
                "code='" + code + '\'' +
                ", msg='" + msg + '\'' +
                ", data='" + data + '\'' +
                '}';
    }

    public Response(String code, String msg, String data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public Response() {
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public String getData() {
        return data;
    }

    public void setData(String data) {
        this.data = data;
    }
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值