已阅读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年中国按压式米桶行业市场前景预测及投资价值评估分析报告
- 六年级下信息技术教学设计-与小海龟交朋友-认识LOGO系统-云南版
- 2025年中国数控攻牙中心行业市场规模及投资前景预测分析报告
- 2025年中国新墙体材料市场研究及发展趋势预测
- 2025年中国无人值守机行业市场前景预测及投资价值评估分析报告
- 浙江省人教版历史与社会八年级下册8.2《洋务运动与近代民族工业的发展》教学设计1
- 2025年中国棉花加工机械市场竞争战略研究及投资前景预测报告
- 2025年中国橡胶助剂行业全景调研及未来发展趋势报告
- 2025年中国毛绒玩具投资战略研究报告
- 第1课 寻找信息科技(教学设计)2024-2025学年三年级上册信息技术人教版
- (完整版)保安培训课件
- 智能信贷系统应用-深度研究
- 信息安全技术保障措施
- 养殖场巡查制度模板
- 【MOOC】中医与辨证-暨南大学 中国大学慕课MOOC答案
- 中建深基坑工程土方开挖专项施工方案
- 《行业会计比较》教案
- 江苏省南通市海门区2024-2025学年八年级上学期11月期中考试(第二届学生学科素养竞赛)英语试卷
- 山东省菏泽市第一中学2025届高一物理第一学期期中质量检测试题含解析
- 公路工程劳务分包指导价
评论
0/150
提交评论