栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

明确使用LambdaMetafactory

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

明确使用LambdaMetafactory

Runnable和Supplier之间的区别在于Supplier使用通用类型。

在运行时,供应商没有String get()方法,而有Object get()。但是您实现的方法返回一个String。您需要区分这两种类型。像这样:

public class metafactoryTest {    public static void main(String[] args) throws Throwable {        MethodHandles.Lookup caller = MethodHandles.lookup();        MethodType methodType = MethodType.methodType(Object.class);        MethodType actualMethodType = MethodType.methodType(String.class);        MethodType invokedType = MethodType.methodType(Supplier.class);        CallSite site = Lambdametafactory.metafactory(caller,"get",invokedType,methodType,caller.findStatic(metafactoryTest.class, "print", actualMethodType),methodType);        MethodHandle factory = site.getTarget();        Supplier<String> r = (Supplier<String>) factory.invoke();        System.out.println(r.get());    }    private static String print() {        return "hello world";    }    }


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/438175.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号