如何创建一个变量 /属性以将相同的参数传递给每个方法?

发布于 2025-02-06 10:31:38 字数 743 浏览 1 评论 0原文

我正在创建一个开关语句,该语句调用每种情况的不同方法。警告是,这些方法都以完全相同的参数为例,因此我发现自己复制了代码。也不可能将这些方法结合在一起 - 因为它们都实例化了唯一的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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

感情废物 2025-02-13 10:31:38

您为什么不像这样那样去做:

getAccount(account, name, address, location, accountID, business);

然后在方法中处理它?

why don't you just go like this :

getAccount(account, name, address, location, accountID, business);

and then handle it inside your method ?

寄意 2025-02-13 10:31:38

您为什么不只是在类(作为实例变量)内部但在方法之外声明变量(名称,地址,位置,accountID,业务)。然后只需在没有输入参数的情况下调用这些方法即可。

switch(account){

      case "25":
        getAccount25();
        break;
      case "26":
        getAccount26();
        break;
      case "27":
        getAccount27();
        break;
      case "28":
        getAccount28();
        break;
      case "29":
        getAccount29();
        break;
}

只要变量在同一类中,这些方法就可以使用这些变量。如果您在其他类中声明这些方法,请将变量“公共静态”。

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.

switch(account){

      case "25":
        getAccount25();
        break;
      case "26":
        getAccount26();
        break;
      case "27":
        getAccount27();
        break;
      case "28":
        getAccount28();
        break;
      case "29":
        getAccount29();
        break;
}

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".

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文