java/android的链式写法

常见类set/get写法与调用

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());

链式写法与调用

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) {
    this.ownerId = 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 链式调用”)
.setType(MsgInfo.Type.TEXT)
.setDirect(MsgInfo.Direct.SEND)
.setStatus(MsgInfo.Status.SENDING)
.setTime(System.currentTimeMillis());

两者区别在于

 传统写法较对于sdk或者工具类的开发者来说较为简介,便于调试,调用者也能在编译器的帮助下了解该类的属性和方法,但是实现某一功能的流程并不能清晰展示。
 链式写法相应增加sdk或者工具类的开发者工作量,但对于调用者而言,逻辑更加清晰与命令,便于代码的维护。

猜你喜欢

转载自blog.csdn.net/qq_27688259/article/details/78582912