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 java defines Union classes to realize the coexistence of data bodies

2025-01-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article is about how java defines Union classes to achieve the coexistence of data bodies. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

Define Union class to realize the coexistence of data bodies.

In struct + language, union, also known as common body, is a data structure similar to struct. Union, like struct, can contain many data types and variables. The differences between the two are as follows:

All variables in the structure (struct) "coexist", all variables take effect, and each variable occupies different memory space.

In union, variables are "mutually exclusive", and only one variable takes effect, and all variables occupy the same block of memory space.

When multiple data needs shared memory or only takes one of them at a time, union can be used.

In the Java language, there is no concept of union and struct, only the concept of class. As we all know, struct can be implemented by class. In fact, union can also be implemented with class. However, this class does not have the function of "multiple data needs to be shared in memory", only "multiple data only one at a time" function.

Here, take the customer message of Wechat agreement as an example. According to my years of experience in interface protocol encapsulation, there are mainly two ways to implement it.

1. Using function to implement Union

The Union class implementation:

/ * * customer message class * / @ ToStringpublic class CustomerMessage {/ * attribute related * / / * * message type * / private String msgType; / * * target user * / private String toUser; / * * Commons related * / / * * News content * / private News news . / * constant dependent * / / * * News news * / public static final String MSG_TYPE_NEWS = "news"; / * * Constructor * / public CustomerMessage () {} / * * Constructor * / public CustomerMessage (String toUser) {this.toUser = toUser } / * * Constructor * / public CustomerMessage (String toUser, News news) {this.toUser = toUser; this.msgType = MSG_TYPE_NEWS; this.news = news;} / * * clear message content * / private void removeMsgContent () {/ / check message type if (Objects.isNull (msgType)) {return } / / clear message content if (MSG_TYPE_NEWS.equals (msgType)) {news = null;} else if (...) {...} msgType = null } / * * check message type * / private void checkMsgType (String msgType) {/ / check message type if (Objects.isNull (msgType)) {throw new IllegalArgumentException ("message type is empty") } / / compare message type if (! Objects.equals (msgType, this.msgType)) {throw new IllegalArgumentException ("message type mismatch");}} / * * set message type function * / public void setMsgType (String msgType) {/ / clear message content removeMsgContent () / / check the message type if (Objects.isNull (msgType)) {throw new IllegalArgumentException ("message type is empty");} / / assign message content this.msgType = msgType; if (MSG_TYPE_NEWS.equals (msgType)) {news = new News () } else if (...) {...} else {throw new IllegalArgumentException ("message type is not supported");}} / * * get message type * / public String getMsgType () {/ / check message type if (Objects.isNull (msgType)) {throw new IllegalArgumentException ("invalid message type") } / / return message type return this.msgType;} / * * set news * / public void setNews (News news) {/ / clear message content removeMsgContent (); / / assign message content this.msgType = MSG_TYPE_NEWS; this.news = news } / * * get news * / public News getNews () {/ / check message type checkMsgType (MSG_TYPE_NEWS); / / return message content return this.news;}.}

The Union class uses:

String accessToken =...; String toUser =...; List articleList =...; News news = new News (articleList); CustomerMessage customerMessage = new CustomerMessage (toUser, news); wechatApi.sendCustomerMessage (accessToken, customerMessage)

Main advantages and disadvantages:

Pros: closer to union, a combination of Cstroke + languages.

Disadvantages: the implementation logic is more complex, and there are many parameter type verification.

two。 Using inheritance to implement Union

The Union class implementation:

/ * * customer message class * / @ Getter@Setter@ToStringpublic abstract class CustomerMessage {/ * attribute related * / / * * message type * / private String msgType; / * * target user * / private String toUser; / * * constant related * / / * * News message * / public static final String MSG_TYPE_NEWS = "news" / * * Constructor * / public CustomerMessage (String msgType) {this.msgType = msgType;} / * * Constructor * / public CustomerMessage (String msgType, String toUser) {this.msgType = msgType; this.toUser = toUser }} / * * News customer message class * / @ Getter@Setter@ToString (callSuper = true) public class NewsCustomerMessage extends CustomerMessage {/ * * attribute related * / / * * News content * / private News news; / * * constructor * / public NewsCustomerMessage () {super (MSG_TYPE_NEWS) } / * * constructor * / public NewsCustomerMessage (String toUser, News news) {super (MSG_TYPE_NEWS, toUser); this.news = news;}}

The Union class uses:

String accessToken =...; String toUser =...; List articleList =...; News news = new News (articleList); CustomerMessage customerMessage = new NewsCustomerMessage (toUser, news); wechatApi.sendCustomerMessage (accessToken, customerMessage)

Main advantages and disadvantages:

Advantages: use virtual base classes and subclasses to split, and the concept of each subclass object is clear

Cons: it is quite different from the union of the CumberCraft + language, but it is roughly the same in function.

The federation does not include the current data type of the federation in the CAccord + language. However, in the Java consortium implemented above, the data type corresponding to the union is already included. So, strictly speaking, the Java consortium is not a real union, but a class with the function of "multiple data one at a time".

Thank you for reading! This is the end of this article on "how java defines Union classes to achieve the coexistence of data bodies". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it out for more people to see!

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

Development

Wechat

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

12
Report