引言
大家好,小面又和大家见面了。相信大家都有可能用到链式调用,比如单元测中的使用:
@Test public void test() { // 连续使用多个eq()的情况 LambdaQueryChainWrapper wrapper1 = PowerMockito.mock(LambdaQueryChainWrapper.class); LambdaQueryChainWrapper wrapper2 = PowerMockito.mock(LambdaQueryChainWrapper.class); LambdaQueryChainWrapper wrapper3 = PowerMockito.mock(LambdaQueryChainWrapper.class); LambdaQueryChainWrapper wrapper4 = PowerMockito.mock(LambdaQueryChainWrapper.class); LambdaQueryChainWrapper wrapper5 = PowerMockito.mock(LambdaQueryChainWrapper.class); List list = PowerMockito.mock(ArrayList.class); PowerMockito.doReturn(wrapper1).when(resourceSubOrderRepository).lambdaQuery(); // eq ()三个参数的情况 ,anyBoolean() 匹配布尔型 PowerMockito.doReturn(wrapper2).when(wrapper1).eq(anyBoolean(), any(), any()); PowerMockito.doReturn(wrapper3).when(wrapper2).eq(anyBoolean(), any(), any()); PowerMockito.doReturn(wrapper4).when(wrapper3).eq(any(), any()); PowerMockito.doReturn(wrapper5).when(wrapper4).eq(any(), any()); PowerMockito.doReturn(list).when(wrapper5).list(); ResponseResult > xx = controller.getInstanceByProductAndRegion(new UserBO(), "00", "xx", "111"); System.out.println(xx.getData()); Assert.assertEquals(new ArrayList<>(), xx.getData()); }
我们可以看到有这种.doXXX.whenXXX.eqXXX这种链式调用法,接下来小面就带你1分钟学会自己写链式调用。
构建对象属性
大家日常在开发创建对象必然都会使用get、set方法。
比如我们创建一个Person类,里面有关于他的多个属性,并写好get、set方法。
public class Person { private String name; private Integer age; private String card; private Integer mathScore; private Integer chineseScore; private Integer chemistryScore; private Integer physicalScore; private Integer biologicalScore; private Integer politicalScore; private Integer historicalScore; private Integer geographyScore; private Integer englishScore; public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public String getCard() { return card; } public void setCard(String card) { this.card = card; } public Integer getMathScore() { return mathScore; } public void setMathScore(Integer mathScore) { this.mathScore = mathScore; } public Integer getChineseScore() { return chineseScore; } public void setChineseScore(Integer chineseScore) { this.chineseScore = chineseScore; } public Integer getChemistryScore() { return chemistryScore; } public void setChemistryScore(Integer chemistryScore) { this.chemistryScore = chemistryScore; } public Integer getPhysicalScore() { return physicalScore; } public void setPhysicalScore(Integer physicalScore) { this.physicalScore = physicalScore; } public Integer getBiologicalScore() { return biologicalScore; } public void setBiologicalScore(Integer biologicalScore) { this.biologicalScore = biologicalScore; } public Integer getPoliticalScore() { return politicalScore; } public void setPoliticalScore(Integer politicalScore) { this.politicalScore = politicalScore; } public Integer getHistoricalScore() { return historicalScore; } public void setHistoricalScore(Integer historicalScore) { this.historicalScore = historicalScore; } public Integer getGeographyScore() { return geographyScore; } public void setGeographyScore(Integer geographyScore) { this.geographyScore = geographyScore; } public Integer getEnglishScore() { return englishScore; } public void setEnglishScore(Integer englishScore) { this.englishScore = englishScore; } @Override public String toString() { return "Person{" + "name='" + name + '\'' + ", age=" + age + ", card='" + card + '\'' + ", mathScore=" + mathScore + ", chineseScore=" + chineseScore + ", chemistryScore=" + chemistryScore + ", physicalScore=" + physicalScore + ", biologicalScore=" + biologicalScore + ", politicalScore=" + politicalScore + ", historicalScore=" + historicalScore + ", geographyScore=" + geographyScore + ", englishScore=" + englishScore + '}'; } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Person person = (Person) o; return Objects.equals(name, person.name) && Objects.equals(age, person.age) && Objects.equals(card, person.card) && Objects.equals(mathScore, person.mathScore) && Objects.equals(chineseScore, person.chineseScore) && Objects.equals(chemistryScore, person.chemistryScore) && Objects.equals(physicalScore, person.physicalScore) && Objects.equals(biologicalScore, person.biologicalScore) && Objects.equals(politicalScore, person.politicalScore) && Objects.equals(historicalScore, person.historicalScore) && Objects.equals(geographyScore, person.geographyScore) && Objects.equals(englishScore, person.englishScore); } @Override public int hashCode() { return Objects.hash(name, age, card, mathScore, chineseScore, chemistryScore, physicalScore, biologicalScore, politicalScore, historicalScore, geographyScore, englishScore); } }
我们在给它设置属性时,使用set方法
public static void main(String[] args) { Person person = new Person(); person.setAge(15); person.setCard("12345"); person.setBiologicalScore(50); person.setChemistryScore(20); person.setChineseScore(100); person.setGeographyScore(80); person.setHistoricalScore(90); person.setEnglishScore(10); person.setMathScore(99); }
有多少个属性,我们就得手写多少次对吧,如果属性的数量比较多,那么就会写很多,有没有一种舒服点的姿势呢?
lombok的链式调用
那就是链式调用,我们先来直接使用lombok提供的方式。
首先还是创建Person类,添加lombok相关注解
@Data @Builder @AllArgsConstructor @NoArgsConstructor public class Person2 { private String name; private Integer age; private String card; private Integer mathScore; private Integer chineseScore; private Integer chemistryScore; private Integer physicalScore; private Integer biologicalScore; private Integer politicalScore; private Integer historicalScore; private Integer geographyScore; private Integer englishScore; }
创建对象赋予属性
public static void main(String[] args) { //链式调用 Person2.Person2Builder builder = Person2.builder(); Person2 p2 = builder.age(15) .englishScore(100) .biologicalScore(100) .card("12345") .geographyScore(100) .historicalScore(100) .chineseScore(100) .build(); Integer age = p2.getAge(); }
我们可以看到效果就是在直观视觉上看起来它的属性更规整,并且是一条链式语句,使用起来就更方便。
public static void main(String[] args) { Person person = new Person(); person.setAge(15); person.setCard("12345"); person.setBiologicalScore(50); person.setChemistryScore(20); person.setChineseScore(100); person.setGeographyScore(80); person.setHistoricalScore(90); person.setEnglishScore(10); person.setMathScore(99); //链式调用 Person2.Person2Builder builder = Person2.builder(); Person2 p2 = builder.age(15) .englishScore(100) .biologicalScore(100) .card("12345") .geographyScore(100) .historicalScore(100) .chineseScore(100) .build(); Integer age = p2.getAge(); }
两种使用方式的的对比显而易见,lombok是靠这个@Builder注解实现对应链式调用,接下来下面也就这个builder实现一下。
自己手动实现链式调用
首先还是创建Person类,添加set、get方法,增加有参无参构造方法
public class Person3 { private String name; private Integer age; private String card; private Integer mathScore; private Integer chineseScore; private Integer chemistryScore; private Integer physicalScore; private Integer biologicalScore; private Integer politicalScore; private Integer historicalScore; private Integer geographyScore; private Integer englishScore; public Person3() { } public Person3(String name, Integer age, String card, Integer mathScore, Integer chineseScore, Integer chemistryScore, Integer physicalScore, Integer biologicalScore, Integer politicalScore, Integer historicalScore, Integer geographyScore, Integer englishScore) { this.name = name; this.age = age; this.card = card; this.mathScore = mathScore; this.chineseScore = chineseScore; this.chemistryScore = chemistryScore; this.physicalScore = physicalScore; this.biologicalScore = biologicalScore; this.politicalScore = politicalScore; this.historicalScore = historicalScore; this.geographyScore = geographyScore; this.englishScore = englishScore; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public String getCard() { return card; } public void setCard(String card) { this.card = card; } public Integer getMathScore() { return mathScore; } public void setMathScore(Integer mathScore) { this.mathScore = mathScore; } public Integer getChineseScore() { return chineseScore; } public void setChineseScore(Integer chineseScore) { this.chineseScore = chineseScore; } public Integer getChemistryScore() { return chemistryScore; } public void setChemistryScore(Integer chemistryScore) { this.chemistryScore = chemistryScore; } public Integer getPhysicalScore() { return physicalScore; } public void setPhysicalScore(Integer physicalScore) { this.physicalScore = physicalScore; } public Integer getBiologicalScore() { return biologicalScore; } public void setBiologicalScore(Integer biologicalScore) { this.biologicalScore = biologicalScore; } public Integer getPoliticalScore() { return politicalScore; } public void setPoliticalScore(Integer politicalScore) { this.politicalScore = politicalScore; } public Integer getHistoricalScore() { return historicalScore; } public void setHistoricalScore(Integer historicalScore) { this.historicalScore = historicalScore; } public Integer getGeographyScore() { return geographyScore; } public void setGeographyScore(Integer geographyScore) { this.geographyScore = geographyScore; } public Integer getEnglishScore() { return englishScore; } public void setEnglishScore(Integer englishScore) { this.englishScore = englishScore; } @Override public String toString() { return "Person{" + "name='" + name + '\'' + ", age=" + age + ", card='" + card + '\'' + ", mathScore=" + mathScore + ", chineseScore=" + chineseScore + ", chemistryScore=" + chemistryScore + ", physicalScore=" + physicalScore + ", biologicalScore=" + biologicalScore + ", politicalScore=" + politicalScore + ", historicalScore=" + historicalScore + ", geographyScore=" + geographyScore + ", englishScore=" + englishScore + '}'; } }
接着在Person3中添加一个静态内部类Builder,添加上Person3中的所有属性
public static class Builder{ private String name; private Integer age; private String card; private Integer mathScore; private Integer chineseScore; private Integer chemistryScore; private Integer physicalScore; private Integer biologicalScore; private Integer politicalScore; private Integer historicalScore; private Integer geographyScore; private Integer englishScore; }
然后创建一个方法,方法名就是属性字段名如name,添加对应参数String name,赋值name,返回Builder自身
public Builder name(String name){ this.name = name; return this; }
其他属性也都如此
public static class Builder{ private String name; private Integer age; private String card; private Integer mathScore; private Integer chineseScore; private Integer chemistryScore; private Integer physicalScore; private Integer biologicalScore; private Integer politicalScore; private Integer historicalScore; private Integer geographyScore; private Integer englishScore; public Builder name(String name){ this.name = name; return this; } public Builder age(Integer age){ this.age = age; return this; } public Builder card(String card){ this.card = card; return this; } public Builder mathScore(Integer mathScore){ this.mathScore = mathScore; return this; } public Builder chineseScore(Integer chineseScore){ this.chineseScore = chineseScore; return this; } public Builder chemistryScore(Integer chemistryScore){ this.chemistryScore = chemistryScore; return this; } public Builder physicalScore(Integer physicalScore){ this.physicalScore = physicalScore; return this; } public Builder biologicalScore(Integer biologicalScore){ this.biologicalScore = biologicalScore; return this; } public Builder politicalScore(Integer politicalScore){ this.politicalScore = politicalScore; return this; } public Builder historicalScore(Integer historicalScore){ this.historicalScore = historicalScore; return this; } public Builder geographyScore(Integer geographyScore){ this.geographyScore = geographyScore; return this; } public Builder englishScore(Integer englishScore){ this.englishScore = englishScore; return this; } }
创建build()方法返回Person3
public Person3 build(){ return new Person3(this.name,this.age,this.card,this.mathScore,this.chineseScore,this.chemistryScore,this.physicalScore,this.biologicalScore,this.politicalScore,this.historicalScore,this.geographyScore,this.englishScore); }
这个时候已经构建完成了
public class Person3 { private String name; private Integer age; private String card; private Integer mathScore; private Integer chineseScore; private Integer chemistryScore; private Integer physicalScore; private Integer biologicalScore; private Integer politicalScore; private Integer historicalScore; private Integer geographyScore; private Integer englishScore; public Person3() { } public Person3(String name, Integer age, String card, Integer mathScore, Integer chineseScore, Integer chemistryScore, Integer physicalScore, Integer biologicalScore, Integer politicalScore, Integer historicalScore, Integer geographyScore, Integer englishScore) { this.name = name; this.age = age; this.card = card; this.mathScore = mathScore; this.chineseScore = chineseScore; this.chemistryScore = chemistryScore; this.physicalScore = physicalScore; this.biologicalScore = biologicalScore; this.politicalScore = politicalScore; this.historicalScore = historicalScore; this.geographyScore = geographyScore; this.englishScore = englishScore; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public String getCard() { return card; } public void setCard(String card) { this.card = card; } public Integer getMathScore() { return mathScore; } public void setMathScore(Integer mathScore) { this.mathScore = mathScore; } public Integer getChineseScore() { return chineseScore; } public void setChineseScore(Integer chineseScore) { this.chineseScore = chineseScore; } public Integer getChemistryScore() { return chemistryScore; } public void setChemistryScore(Integer chemistryScore) { this.chemistryScore = chemistryScore; } public Integer getPhysicalScore() { return physicalScore; } public void setPhysicalScore(Integer physicalScore) { this.physicalScore = physicalScore; } public Integer getBiologicalScore() { return biologicalScore; } public void setBiologicalScore(Integer biologicalScore) { this.biologicalScore = biologicalScore; } public Integer getPoliticalScore() { return politicalScore; } public void setPoliticalScore(Integer politicalScore) { this.politicalScore = politicalScore; } public Integer getHistoricalScore() { return historicalScore; } public void setHistoricalScore(Integer historicalScore) { this.historicalScore = historicalScore; } public Integer getGeographyScore() { return geographyScore; } public void setGeographyScore(Integer geographyScore) { this.geographyScore = geographyScore; } public Integer getEnglishScore() { return englishScore; } public void setEnglishScore(Integer englishScore) { this.englishScore = englishScore; } @Override public String toString() { return "Person{" + "name='" + name + '\'' + ", age=" + age + ", card='" + card + '\'' + ", mathScore=" + mathScore + ", chineseScore=" + chineseScore + ", chemistryScore=" + chemistryScore + ", physicalScore=" + physicalScore + ", biologicalScore=" + biologicalScore + ", politicalScore=" + politicalScore + ", historicalScore=" + historicalScore + ", geographyScore=" + geographyScore + ", englishScore=" + englishScore + '}'; } public static class Builder{ private String name; private Integer age; private String card; private Integer mathScore; private Integer chineseScore; private Integer chemistryScore; private Integer physicalScore; private Integer biologicalScore; private Integer politicalScore; private Integer historicalScore; private Integer geographyScore; private Integer englishScore; public Builder name(String name){ this.name = name; return this; } public Builder age(Integer age){ this.age = age; return this; } public Builder card(String card){ this.card = card; return this; } public Builder mathScore(Integer mathScore){ this.mathScore = mathScore; return this; } public Builder chineseScore(Integer chineseScore){ this.chineseScore = chineseScore; return this; } public Builder chemistryScore(Integer chemistryScore){ this.chemistryScore = chemistryScore; return this; } public Builder physicalScore(Integer physicalScore){ this.physicalScore = physicalScore; return this; } public Builder biologicalScore(Integer biologicalScore){ this.biologicalScore = biologicalScore; return this; } public Builder politicalScore(Integer politicalScore){ this.politicalScore = politicalScore; return this; } public Builder historicalScore(Integer historicalScore){ this.historicalScore = historicalScore; return this; } public Builder geographyScore(Integer geographyScore){ this.geographyScore = geographyScore; return this; } public Builder englishScore(Integer englishScore){ this.englishScore = englishScore; return this; } public Person3 build(){ return new Person3(this.name,this.age,this.card,this.mathScore,this.chineseScore,this.chemistryScore,this.physicalScore,this.biologicalScore,this.politicalScore,this.historicalScore,this.geographyScore,this.englishScore); } } }
直接使用
public static void main(String[] args) { //自己实现builder Person3 p3 = new Person3.Builder() .age(18) .card("12345") .mathScore(100) .chemistryScore(100) .biologicalScore(100) .build(); Integer p3Age = p3.getAge(); }
可以看到效果和lombok实现的方式一样,我们自己也可以链式调用自己的对象了。
总结
小面今天的教学到这就结束啦,相信你已经学会了自己如何构建链式调用的方法,后面代码实操可以用起来,能在工作中对你有所帮助的。