问答文章1 问答文章501 问答文章1001 问答文章1501 问答文章2001 问答文章2501 问答文章3001 问答文章3501 问答文章4001 问答文章4501 问答文章5001 问答文章5501 问答文章6001 问答文章6501 问答文章7001 问答文章7501 问答文章8001 问答文章8501 问答文章9001 问答文章9501

Java方法参数太多怎么办

发布网友 发布时间:2022-04-27 06:58

我来回答

1个回答

热心网友 时间:2022-04-22 20:53

引入参数对象

Person.class

public class Person {
public FullName fullName;
public Address address;
public boolean isFemale;
public boolean isEmployed;
public boolean isHomeOwner;
public Person(FullName fullName, Address address, boolean isFemale, boolean isEmployed, boolean isHomeOwner) {
this.fullName = fullName;
this.address = address;
this.isFemale = isFemale;
this.isEmployed = isEmployed;
this.isHomeOwner = isHomeOwner;
}
}

FullName.class

public class FullName {
public String lastName;
public String firstName;
public String middleName;
public String salutation;
public String suffix;
public FullName(String lastName, String firstName, String middleName, String salutation, String suffix) {
this.lastName = lastName;
this.firstName = firstName;
this.middleName = middleName;
this.salutation = salutation;
this.suffix = suffix;
}
}

Address.class

public class Address {
public String streetAddress;
public String city;
public String state;
public Address(String streetAddress, String city, String state) {
this.streetAddress = streetAddress;
this.city = city;
this.state = state;
}
}

问题:参数对象可能被滥用。如果一个开发者纯粹为了减少参数数量,把联系不紧的几个参数强捆在一个类中这肯定是行不通的,在可读性上甚至适得其反。

参数关系紧密合并到一个对象中

Builder模式:

Address.class
优点:客户端代码的可用性和可读性得到了大大提高,构造函数的参数数量明显减少调用起来非常直观。单个builder构建多个对象时Builder参数可在创建期间进行调整,还可以根据对象不同而进行改变,有效的避免重载构造函数。
缺点:增加代码量,代码变得更加冗长(相比较参数数量的增加,相同类型的参数混在一起,可选参数的增加而言,改善代码可读性更有价值)
Person.class
需求:当一个对象需要不同的参数构造方法?不能写5*5满足所有的需求吧
适用范围:构建对象时,如果碰到类有很多参数——其中很多参数类型相同而且很多参数可以为空时,使用Builder模式来完成。当参数数量不多、类型不同而且都是必须出现时,通过增加代码实现Builder往往无法体现它的优势。在这种情况下,理想的方法是调用传统的构造函数。再者,如果不需要保持不变,那么就使用无参构造函数调用相应的set方法吧。

代码

public class Person {
public FullName fullName;
public Address address;
public boolean isFemale;
public boolean isEmployed;
public boolean isHomeOwner;

public Person(FullName fullName, Address address, boolean isFemale, boolean isEmployed, boolean isHomeOwner) {
this.fullName = fullName;
this.address = address;
this.isFemale = isFemale;
this.isEmployed = isEmployed;
this.isHomeOwner = isHomeOwner;
}

public static class Builder {
private FullName fullName;
private Address address;
private boolean isFemale;
private boolean isEmployed;
private boolean isHomeOwner;

/**
* 如果有必填参数这里可以构造必填构造方法
*/
public Builder() {
}

public Builder setFullName(FullName fullName) {
this.fullName = fullName;
return this;
}

public Builder setAddress(Address address) {
this.address = address;
return this;
}

public Builder setFemale(boolean female) {
isFemale = female;
return this;
}

public Builder setEmployed(boolean employed) {
isEmployed = employed;
return this;
}

public Builder setHomeOwner(boolean homeOwner) {
isHomeOwner = homeOwner;
return this;
}

public Person create() {
return new Person(fullName, address, isFemale, isEmployed, isHomeOwner);
}
}
}

FullName.class

public class FullName {
public String lastName;
public String firstName;
public String middleName;
public String salutation;
public String suffix;

public FullName(String lastName, String firstName, String middleName, String salutation, String suffix) {
this.lastName = lastName;
this.firstName = firstName;
this.middleName = middleName;
this.salutation = salutation;
this.suffix = suffix;
}

public static class Builder {
private String lastName;
private String firstName;
private String middleName;
private String salutation;
private String suffix;

public Builder() {
}

public Builder setLastName(String lastName) {
this.lastName = lastName;
return this;
}

public Builder setFirstName(String firstName) {
this.firstName = firstName;
return this;
}

public Builder setMiddleName(String middleName) {
this.middleName = middleName;
return this;
}

public Builder setSalutation(String salutation) {
this.salutation = salutation;
return this;
}

public Builder setSuffix(String suffix) {
this.suffix = suffix;
return this;
}

public FullName create() {
return new FullName(lastName, firstName, middleName, salutation, suffix);
}
}
}

public class Address {
public String streetAddress;
public String city;
public String state;
public Address(String streetAddress, String city, String state) {
this.streetAddress = streetAddress;
this.city = city;
this.state = state;
}
public static class Builder {
private String streetAddress;
private String city;
private String state;

public Builder() {
}

public Builder setStreetAddress(String streetAddress) {
this.streetAddress = streetAddress;
return this;
}

public Builder setCity(String city) {
this.city = city;
return this;
}

public Builder setState(String state) {
this.state = state;
return this;
}

public Address create() {
return new Address(streetAddress, city, state);
}
}
}

调用的地方

public static void main(String[] args) {
FullName fullName = new FullName.Builder().setFirstName("yes")
.setLastName("no").create();
Address address = new Address.Builder().setCity("china").setState("12")
.create();
Person person = new Person.Builder().setAddress(address)
.setFullName(fullName).create();
}

重载

适用范围:方法中参数可选参数或者参数中指定参数相同

代码

public String name(String name,int year) {
return name+year;
}
/**
* 重载
* @param name
* @return
*/
public String name(String name) {
return name+"null";
}

优点:遇到可选参数或者默认参数时,使用方法重载会十分有效。
声明声明:本网页内容为用户发布,旨在传播知识,不代表本网认同其观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。E-MAIL:11247931@qq.com
写论文值得推荐的app 写论文app大全汇总 有啥好用又免费的文献查阅网站吗? 高仿苹果手机关机关不了机?是怎么回事?有那个朋友请告知! 高仿苹果6开机就是白屏黑苹果,关机关不了 怎么把电子相册保存到QQ空间相册 整个乡村音乐史上,最动人的25首歌 写给狗的歌 右侧肢体肌力4级是什么意思 肌力分级的评定标准 肌力4级是什么意思 java方法参数 Java中的方法通常包含三个部分,分别是___、参数列表 JAVA中类型后面跟三个点是什么来的。 java中能调用参数个数不同的方法? java中一个方法可以有多少个参数 java方法的参数可以是多个吗 java中 replace函数后面有三个参数,是什么作用,什么返回值 要求定义一个getvolume方法,包含3个double型参数用java写该怎么写 java新手提问。这个应该怎么写。三个参数,我不会重写compareTo了。也不知道contain java定义一个方法siZe, 设置三个参数,两个数字为jint型,-一个符号为char型在? java定义一个方法,接受三个int参数,返回最小的一个数. 小牛电动车N1s怎样同时连接2块电池? java 写一个方法,输入参数是3个整型数字,输出是void型,在方法里面不 小牛电动车电池取出怎么查电量 小牛电动车电池只接两根电源线可以走吗? 小牛mqi2解速方法 德龙ec9咖啡机怎么打奶泡? 德龙咖啡机怎么使用 拉皮妥协还是迭代进化?小牛发布新国标电动车MQi2 小牛电动车MQi2车座下面那个黑色横盖是干嘛 一个方法可以有多少个参数多少个返回值 Java Java中KeyListener中三个方法的运行原理是什么?为要一个KeyEvent参数? java中使用接口作为方法的参数,这说明什么?能传什么参数进去? java 方法参数过多,怎么处理 插入java参数方法有多少种? 苹果手机突然没有声音了,怎么回事 如何把只读WORD文档转为可改写状态文档 电脑怎么安装win8系统 怎么安装win8 贩200克k毒可以怎么量刑 麻辣盆的做法 辽宁抚顺麻辣拌的具体做法是? 在抖音上有男生给我作品点赞有什么苹苹果为什么删除了他是什么心理状态 抚顺麻辣拌做法 麻辣拌怎么做的啊 抚顺麻辣盆以及麻辣拌的做法? 抚顺麻辣拌和麻辣盆是一样的吗? 两张PP卡可以带人么? 如何使用旋转蒸发仪蒸馏苯胺,详细步骤 麻辣拌怎么制作?