普通调用和链式调用

1. 定义

重复使用一个初始化操作,来达到少量代码表达复杂操作的目的。

2.表现

(1)用"."连接

OkHttpUtils<NewGoodsBea> utils = new OkHttpUtils<>(context);

utils.setRequestUrl(url)

.addParam("cat_id", "0")

.addParam("page_size", 10)

.targetClass(NewGoodBean.class)

.execute(listener);

备注:OkHttpUtils是基于okhttp3封装额下载框架

(2)方法返回值

public class OhHttpUtils<T>{

成员变量1

成员变量2

....

 

  public OkHttpUtils<T> url(String url){

    mUrl = new StringBuilder(url);

    return this;

  }

}

 

OkHttpUtils的封装框架中,方法的返回值是 this

 

3 普通调用&链式调用

3.1 普通调用

Person.class实现方式

public class Person{

  private String name;

  private int age;

  public Person(){}

  public setName(String name){

    this.name = name;

  }

  public setAge(int age){

    this.age = age;

  }

}

普通调用方式:
Person person = new Person();

person.setName("Tony");

person.setAge(11);

 

3.2链式调用方式

Person.class实现方式

public class Person{

  private String name;

  private String age;

  public Person(){}

  public Person setName(String name){

    this.name = name;

    return this;

  }

  public Person setAge(int age){

    this.age = age;

    return this;

  }

}

链式调用方式:
Person person = new Person()

person.setName("Tony).setAge(11).setName("张三").setName("李四").setAge(12);

 

posted @ 2017-07-29 16:35  风之承  阅读(1057)  评论(0编辑  收藏  举报