In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly explains "the implementation of java enumeration and the definition of interface". The content of the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "the implementation of java enumeration and the definition of interface".
Catalogue
Preface
Example
Enumerated implementation
Interface definition
Realize
General multiple implementation (call example)
Business scenario
Interface definition
Realize
Application
Preface
The purpose of multi-inheritance is to improve the function of subclasses, and the expansibility has been improved.
In order to extend the function of subclasses, java is improved into multiple implementations. Here are two questions. Will there be no uncertainty if there is more implementation? The interface is full of abstract methods, and the implementation of multiple implementation structures has to be rewritten, is that meaningful?
The second problem is to rewrite the method, and the solution of the multi-implementation is to make the implemented subclass have some functionality, and the trouble of rewriting the function is not considered. What about the first question of uncertainty?
Multiple inheritance is not supported because of the uncertainty of the method, the declaration is the same, the method body is different, and the subclass does not know which method body to execute when inheriting and executing the same method. The problem lies in the method. The use of multiple inheritance is prohibited in order to prevent such problems. But all the methods defined in the interface are abstract methods, and when there are multiple inheritance (incorrect statement, it should be described as implementation, but the essence is still inheritance coverage), it doesn't matter if you encounter the same function, because there is no method body in it, and the method body to be executed is the self-rewriting of the subclass, which does not cause the choice of multiple different method bodies at all. Moreover, I don't think the same method will appear in multiple interfaces at all, because it is written from top to bottom. It's just that in the initial understanding, it is from the bottom up, and these problems may be encountered. Multi-inheritance is not executed, and there are general functions in the parent class, not abstract functions. This is not to say that it is not possible for a subclass to inherit more than a few parent classes, but it is prohibited in order to prevent problems.
The interface improves some functions to the subclass, telling the subclass what functions you can have, and as to how to achieve these functions, the subclass needs to write its own.
Interface classes in Java are usually designed to extract common ground, standardize the implementation, make it easy to read, handle multiple implementations of interface classes and provide elegant hit concrete implementations, which can help us simplify the code and improve readability. Here are several comfortable multiple implementations and invocation methods for your reference.
Example
Enumerated implementation
Interface definition
Public interface Breakfast {void eat ();} implementation
Public enum BreakfastEnum implements Breakfast {Beijing ("Beijing") {@ Override public void eat () {System.out.println ("Beijingers eat bean juice and coke circles for breakfast"), Wuhan ("Wuhan") {@ Override public void eat () {System.out.println ("Wuhan people eat hot and dry noodles and bean skins for breakfast.") }}, Unknown ("unknown") {@ Override public void eat () {System.out.println ("No breakfast!") ;}}; private String city; BreakfastEnum (String city) {this.city = city;} private String getCity () {return this.city;} / * provides a unified entry to find the corresponding subclass and execute * * @ param city * / public static void eat (String city) {BreakfastEnum [] values = BreakfastEnum.values () Arrays.stream (values) .filter (e-> city.equals (e.city)). FindFirst (). OrElse (Unknown). Eat ();}}
Test it
Through the enumeration class to implement the interface, each enumeration is equivalent to an implementation, and the method can be implemented in the code block. Finally, a static method is provided in the enumeration class as a unified entry, which is convenient to call, concise in code, and provides a general implementation to deal with scenarios without a specific implementation. It is suitable for replacing more ifelse business codes, optimizing complex tool classes, etc., and be cautious about many methods and complex business.
General multiple implementation (call example)
Business scenario
We have a message service that listens to the message and sends it to the client. There is a publish method field in the message
1. Send to the specified path according to the way in which the message is published
two。 Send messages to all channels
API definition public interface MessageHandle {/ * publish message * * @ param msg * / void publish (JSONObject msg) } implement / * send SMS * / @ Service ("sms") public class SmsMessageHandle implements MessageHandle {@ Override public void publish (JSONObject msg) {/ / send SMS / / omit.}} / * push * / @ Service ("push") public class PushMessageHandle implements MessageHandle {@ Override public void publish (JSONObject msg) {/ / push to App / / omit the implementation.}} Application / / 1. Specify the path to send @ Componentpublic class MessageListener {@ Autowired private Map messageHandleMap; @ KafkaListener (groupId = "message-server", topics = "message") public void listener (String message, Acknowledgment ack) {JSONObject messageJson = JSON.parseObject (message); / / get the publishing method sms push... Corresponding to the name String publishType = messageJson.getString ("publishType") in the @ Service annotation; / / get the implementation MessageHandle handle = messageHandleMap.get (publishType); if (handle! = null) {handle.publish (messageJson);} / / submit offset ack.acknowledge ();}} / / 2. Each route sends @ Componentpublic class MessageListener {@ Autowired private List messageHandleList; @ KafkaListener (groupId = "message-server", topics = "message") public void listener (String message, Acknowledgment ack) {JSONObject messageJson = JSON.parseObject (message); / / each route sends for (MessageHandle handle: messageHandleList) {handle.publish (messageJson) } / / submit offset ack.acknowledge ();}} Thank you for reading. The above is the content of "implementation of java enumeration and definition of interface". After the study of this article, I believe you have a deeper understanding of the implementation of java enumeration and the definition of interface, and the specific usage needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
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: 216
*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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.