首页 > Java > java教程 > 正文

java如何定义Union类实现数据体的共存

王林
发布: 2023-05-14 15:34:06
转载
2563人浏览过

定义Union类实现数据体的共存

在C/C++语言中,联合体(union),又称共用体,类似结构体(struct)的一种数据结构。联合体(union)和结构体(struct)一样,可以包含很多种数据类型和变量,两者区别如下:

  1. 结构体(struct)中所有变量是“共存”的,同时所有变量都生效,各个变量占据不同的内存空间;

  2. 联合体(union)中是各变量是“互斥”的,同时只有一个变量生效,所有变量占据同一块内存空间。

当多个数据需要共享内存或者多个数据每次只取其一时,可以采用联合体(union)。

在Java语言中,没有联合体(union)和结构体(struct)概念,只有类(class)的概念。众所众知,结构体(struct)可以用类(class)来实现。其实,联合体(union)也可以用类(class)来实现。但是,这个类不具备“多个数据需要共享内存”的功能,只具备“多个数据每次只取其一”的功能。

这里,以微信协议的客户消息为例说明。根据我多年来的接口协议封装经验,主要有以下两种实现方式。

1.使用函数方式实现Union

Union类实现:

/** 客户消息类 */@ToStringpublic class CustomerMessage {    /** 属性相关 */
    /** 消息类型 */
    private String msgType;    /** 目标用户 */
    private String toUser;    /** 共用体相关 */
    /** 新闻内容 */
    private News news;
    ...    /** 常量相关 */
    /** 新闻消息 */
    public static final String MSG_TYPE_NEWS = "news";
    ...    /** 构造函数 */
    public CustomerMessage() {}    /** 构造函数 */
    public CustomerMessage(String toUser) {        this.toUser = toUser;
    }    /** 构造函数 */
    public CustomerMessage(String toUser, News news) {        this.toUser = toUser;        this.msgType = MSG_TYPE_NEWS;        this.news = news;
    }    /** 清除消息内容 */
    private void removeMsgContent() {        // 检查消息类型
        if (Objects.isNull(msgType)) {            return;
        }        // 清除消息内容
        if (MSG_TYPE_NEWS.equals(msgType)) {
            news = null;
        } else if (...) {
            ...
        }
        msgType = null;
    }    /** 检查消息类型 */
    private void checkMsgType(String msgType) {        // 检查消息类型
        if (Objects.isNull(msgType)) {            throw new IllegalArgumentException("消息类型为空");
        }        // 比较消息类型
        if (!Objects.equals(msgType, this.msgType)) {            throw new IllegalArgumentException("消息类型不匹配");
        }
    }    /** 设置消息类型函数 */
    public void setMsgType(String msgType) {        // 清除消息内容
        removeMsgContent();        // 检查消息类型
        if (Objects.isNull(msgType)) {            throw new IllegalArgumentException("消息类型为空");
        }        // 赋值消息内容
        this.msgType = msgType;        if (MSG_TYPE_NEWS.equals(msgType)) {
            news = new News();
        } else if (...) {
            ...
        } else {            throw new IllegalArgumentException("消息类型不支持");
        }
    }    /** 获取消息类型 */
    public String getMsgType() {        // 检查消息类型
        if (Objects.isNull(msgType)) {            throw new IllegalArgumentException("消息类型无效");
        }        // 返回消息类型
        return this.msgType;
    }    /** 设置新闻 */
    public void setNews(News news) {        // 清除消息内容
        removeMsgContent();        // 赋值消息内容
        this.msgType = MSG_TYPE_NEWS;        this.news = news;
    }    /** 获取新闻 */
    public News getNews() {        // 检查消息类型
        checkMsgType(MSG_TYPE_NEWS);        // 返回消息内容
        return this.news;
    }
    
    ...
}
登录后复制

Union类使用:

String accessToken = ...;
String toUser = ...;
List<Article> articleList = ...;
News news = new News(articleList);
CustomerMessage customerMessage = new CustomerMessage(toUser, news);
wechatApi.sendCustomerMessage(accessToken, customerMessage);
登录后复制

主要优缺点:

  • 优点:更贴近C/C++语言的联合体(union);

  • 缺点:实现逻辑较为复杂,参数类型验证较多。

2.使用继承方式实现Union

Union类实现:

/** 客户消息类 */@Getter@Setter@ToStringpublic abstract class CustomerMessage {    /** 属性相关 */
    /** 消息类型 */
    private String msgType;    /** 目标用户 */
    private String toUser;    /** 常量相关 */
    /** 新闻消息 */
    public static final String MSG_TYPE_NEWS = "news";
    ...    /** 构造函数 */
    public CustomerMessage(String msgType) {        this.msgType = msgType;
    }    /** 构造函数 */
    public CustomerMessage(String msgType, String toUser) {        this.msgType = msgType;        this.toUser = toUser;
    }
}/** 新闻客户消息类 */@Getter@Setter@ToString(callSuper = true)public class NewsCustomerMessage extends CustomerMessage {    /** 属性相关 */
    /** 新闻内容 */
    private News news;    /** 构造函数 */
    public NewsCustomerMessage() {        super(MSG_TYPE_NEWS);
    }    /** 构造函数 */
    public NewsCustomerMessage(String toUser, News news) {        super(MSG_TYPE_NEWS, toUser);        this.news = news;
    }
}
登录后复制

Union类使用:

String accessToken = ...;
String toUser = ...;
List<Article> articleList = ...;
News news = new News(articleList);
CustomerMessage customerMessage = new NewsCustomerMessage(toUser, news);
wechatApi.sendCustomerMessage(accessToken, customerMessage);
登录后复制

主要优缺点:

  • 优点:使用虚基类和子类进行拆分,各个子类对象的概念明确;

    立即学习Java免费学习笔记(深入)”;

  • 缺点:与C/C++语言的联合体(union)差别大,但是功能上大体一致。

在C/C++语言中,联合体并不包括联合体当前的数据类型。但在上面实现的Java联合体中,已经包含了联合体对应的数据类型。所以,从严格意义上说,Java联合体并不是真正的联合体,只是一个具备“多个数据每次只取其一”功能的类。

以上就是java如何定义Union类实现数据体的共存的详细内容,更多请关注php中文网其它相关文章!

java速学教程(入门到精通)
java速学教程(入门到精通)

java怎么学习?java怎么入门?java在哪学?java怎么学才快?不用担心,这里为大家提供了java速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载
来源:亿速云网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号