Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to implement method reference in java8

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

In this issue, the editor will bring you about how to implement the method reference in java8. The article is rich in content and analyzes and narrates it from a professional point of view. I hope you can get something after reading this article.

Method reference: the method overridden in the lambda expression is replaced by the existing method.

For example, Chestnut has a function interface implementation.

Java8new3Demo java8new3Demo=new Java8new3Demo () {@ Override public String method1 (String id) {return null;}}

Here is the lambda expression

Java8new3Demo java8new3Demo2=id-> {return id+ "lambda";}

If I am lazier and don't want to rewrite the only abstract method, there happens to be a ready-made method that is the same as the one I want to rewrite. Can I just get it? This is the method reference.

If I had a ready-made class

Public class Java8new3DemoT {public static String getname (String id) {return id+ "Java8new3DemoT";} public String getname2 (String id) {return id+ "Java8new3DemoT";}}

The methods, parameters and return values in this class are the same as the methods to be implemented in the function interface, so we can use them directly.

Java8new3Demo java8new3Demo3=Java8new3DemoT::getname

Previous static method of class name + double colon + reference

If it is a non-static method, you need to create an entity first.

Java8new3DemoT java8new3DemoT=new Java8new3DemoT (); Java8new3Demo java8new3Demo4=java8new3DemoT::getname2; above is the reference of how to implement the java8 method shared by the editor. If you happen to have similar doubts, please refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report