如何创建一个变量 /属性以将相同的参数传递给每个方法?
我正在创建一个开关语句,该语句调用每种情况的不同方法。警告是,这些方法都以完全相同的参数为例,因此我发现自己复制了代码。也不可能将这些方法结合在一起 - 因为它们都实例化了唯一的XML支持实例。
switch(account){
case "25":
getAccount25(name, address, location, accountID, business)
break
case "26":
getAccount26(name, address, location, accountID, business)
break
case "27":
getAccount27(name, address, location, accountID, business)
break
case "28":
getAccount28(name, address, location, accountID, business)
break
case "29":
getAccount29(name, address, location, accountID, business)
break
是否有一种方法可以避免为每个方法调用复制(名称,地址,位置,帐户,业务)?
I'm creating a switch statement that calls different methods for each case. The caveat is, the methods all take in the exact same arguments, so I find myself duplicating code. It isn't possible to combine these methods either - as they all instantiate unique xml backing instances.
switch(account){
case "25":
getAccount25(name, address, location, accountID, business)
break
case "26":
getAccount26(name, address, location, accountID, business)
break
case "27":
getAccount27(name, address, location, accountID, business)
break
case "28":
getAccount28(name, address, location, accountID, business)
break
case "29":
getAccount29(name, address, location, accountID, business)
break
Is there a way to avoid duplicating (name, address, location, accountID, business) for each method call?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
更多
发布评论
评论(2)
您为什么不像这样那样去做:
然后在方法中处理它?
why don't you just go like this :
and then handle it inside your method ?
您为什么不只是在类(作为实例变量)内部但在方法之外声明变量(名称,地址,位置,accountID,业务)。然后只需在没有输入参数的情况下调用这些方法即可。
只要变量在同一类中,这些方法就可以使用这些变量。如果您在其他类中声明这些方法,请将变量“公共静态”。
why don't you just declare the variables (name, address, location, accountID, business) inside the class (as an instance variable) but outside the method. Then just call the methods with no entry argument.
The methods can use the variables as long as they are in the same class. If you are declaring the methods in another class, make the variables "public static".