




已阅读5页,还剩2页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
Android总结之链式调用(方法链)前言: 最近在学习总结Android属性动画的时候,发现Android的属性动画设计采用了链式调用的方式,然后又回顾了一下了以前接触的开源框架Glide也是采用链式调用的方式,还有最近火的一塌糊涂的RxJava也是采用链式调用,为何如此之多的开源项目采用这种设计方式,今天来对比学习一下。什么是链式调用? 链式调用其实只不过是一种语法招数。它能让你通过重用一个初始操作来达到用少量代码表达复杂操作的目的。表现形式: 一个初始化操作之后,后面的调用以“.”连接起来。例如Glide使用Glide.with(this).load(imageUrl).placeholder(R.mipmap.ic_launcher).error(R.mipmap.ic_launcher).into(imageView);实际举例: 以以前做的简单的IM即时通讯消息体MsgInfo为例。1.)普通实现方式MsgInfo.java实现方式复制代码public class MsgInfo /* * 消息的类型 */ public static class Type public final static int TEXT = 0; / 文本消息 public final static int IMAGE = 1; / 图片消息 public final static int VOICE = 2; / 语音消息 public final static int MOVIE = 3;/ 视频消息 public final static int URL = 4;/URL消息 /* * 消息的方向 */ public static class Direct public final static int SEND = 0; / 发送 public final static int RECEIVE = 1; / 接收 /* * 消息的状态 */ public static class Status public final static int SEND_SUCCESS= 0; / 已发送 public final static int SENDING = 1; / 正在发送 public final static int SEND_FAILED = 2; / 发送失败 public final static int READ = 3; / 已读 public final static int UNREAD = 4; / 未读 private long msgId;/消息Id private String ownerId;/消息属于哪个用户 private String relatedId;/消息关联到哪个用户; private String body;/消息体 private long time;/消息发送接收时间 private int direct;/ 消息的方向 private int status;/消息的状态 private int type;/消息的类型 public MsgInfo() public long getMsgId() return msgId; public void setMsgId(long msgId) this.msgId = msgId; public int getType() return type; public void setType(int type) this.type = type; public String getOwnerId() return ownerId; public void setOwnerId(String ownerId) this.ownerId = ownerId; public String getRelatedId() return relatedId; public void setRelatedId(String relatedId) this.relatedId = relatedId; public String getBody() return body; public void setBody(String body) this.body = body; public long getTime() return time; public void setTime(long time) this.time = time; public int getDirect() return direct; public void setDirect(int direct) this.direct = direct; public int getStatus() return status; public void setStatus(int status) this.status = status; 复制代码调用方式复制代码MsgInfo msgInfo = new MsgInfo();msgInfo.setOwnerId(100011002);msgInfo.setRelatedId(1000110003);msgInfo.setBody(hello 普通调用);msgInfo.setType(MsgInfo.Type.TEXT);msgInfo.setDirect(MsgInfo.Direct.SEND);msgInfo.setStatus(MsgInfo.Status.SENDING);msgInfo.setTime(System.currentTimeMillis();复制代码2.)链式调用方式MsgInfo.java实现复制代码public class MsgInfo /* * 消息的类型 */ public static class Type public final static int TEXT = 0; / 文本消息 public final static int IMAGE = 1; / 图片消息 public final static int VOICE = 2; / 语音消息 public final static int MOVIE = 3;/ 视频消息 public final static int URL = 4;/URL消息 /* * 消息的方向 */ public static class Direct public final static int SEND = 0; / 发送 public final static int RECEIVE = 1; / 接收 /* * 消息的状态 */ public static class Status public final static int SEND_SUCCESS= 0; / 已发送 public final static int SENDING = 1; / 正在发送 public final static int SEND_FAILED = 2; / 发送失败 public final static int READ = 3; / 已读 public final static int UNREAD = 4; / 未读 private long msgId;/消息Id private String ownerId;/消息属于哪个用户 private String relatedId;/消息关联到哪个用户; private String body;/消息体 private long time;/消息发送接收时间 private int direct;/ 消息的方向 private int status;/消息的状态 private int type;/消息的类型 public MsgInfo() public long getMsgId() return msgId; public MsgInfo setMsgId(long msgId) this.msgId = msgId; return this; public int getType() return type; public MsgInfo setType(int type) this.type = type; return this; public String getOwnerId() return ownerId; public MsgInfo setOwnerId(String ownerId) TnerId = ownerId; return this; public String getRelatedId() return relatedId; public MsgInfo setRelatedId(String relatedId) this.relatedId = relatedId; return this; public String getBody() return body; public MsgInfo setBody(String body) this.body = body; return this; public long getTime() return time; public MsgInfo setTime(long time) this.time = time; return this; public int getDirect() return direct; public MsgInfo setDirect(int direct) this.direct = direct; return this; public int getStatus() return status; public MsgInfo setStatus(int status) this.status = status; return this; 复制代码调用方式复制代码 MsgInfo msgInfo = new MsgInfo(); msgInfo.setOwnerId(100011002) .setRelatedId(1000110003) .setBody(hello 链式调用
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 中国广电宜春市2025秋招网申填写模板含开放题范文
- 神农架林区中储粮2025秋招购销统计岗高频笔试题库含答案
- 国家能源张家口市2025秋招交通运输类面试追问及参考回答
- 中国移动昆明市2025秋招笔试行测题库及答案技能类
- 公路定额类考试题及答案
- 甘南藏族自治州中石油2025秋招笔试综合知识专练题库及答案
- 大唐电力临汾市2025秋招面试专业追问及参考计算机与信息岗位
- 中国移动广安市2025秋招笔试行测题库及答案综合管理类
- 中国广电济源市2025秋招网络优化与维护类专业追问清单及参考回答
- 中国联通儋州市2025秋招笔试行测经典题及答案
- 小小科学家体验活动-物理三年级(物理)试题含答案
- 多肽药物分析方法开发研究
- 花园小学少先队知识竞赛题
- 2023-2024学年上海市杨浦区六年级上学期期中考试语文试卷含详解
- 农行超级柜台业务知识考试题库(含答案)
- AMC数学竞赛真题答案2023
- 【华帝厨电应收账款现状及其风险分析(论文10000字)】
- 新标准大学英语(第三版)综合教程3(智慧版)课件 Unit6 Path to prosperity
- 部编版语文七年级上册第1课《春》阅读理解题(含解析)
- 短波通信系统和超短波通信系统
- 野外地质调查安全手册
评论
0/150
提交评论