【问题标题】:Method accepting two different types as parameter in JavaJava中接受两种不同类型作为参数的方法
【发布时间】:2020-02-25 13:56:28
【问题描述】:

要求:-

在 getDetails 方法中,如果传递了 Customclass 类型的对象 B,那么我们将调用 getStatus 方法,该方法以 Customclass 作为参数。 现在,我们需要让参数同时接受 string/ customclass 类型,

    so if it is string, then we are directly getting the value so need not to call getStatus method
    And if it of type **customclass**, then we need to invoke getStatus

在我现有的项目中,我们从多个地方调用 getDetails,而 getDetails 是一个冗长的方法,因此重载成本太高,会导致代码重复

请提出其他方法

我有类似下面的代码:-

    getDetails(Strig a, Customclass B) {
      //lengthy calculation long method
      String status = getStatus(B)
     //lengthy calculation long method
   }

我想让它像下面这样:-

   getDetails(Strig a, Customclass B || String B) {
           //lengthy calculation long method
           String status;
            If(B of type String) {
              status = B;
            } else {

               status = getStatus(B)
            }
           //lengthy calculation long method
   }

【问题讨论】:

  • 您可以创建一个自定义类作为 Enum,在这种情况下您可以涵盖这两种情况。
  • 您的建议如何?它是否需要我只在一个地方进行更改,还是我需要在我调用它的任何地方重复代码?有示例代码吗?

标签: java generics polymorphism overloading


【解决方案1】:

创建两个方法getdetails:

getDetails(Strig a, Customclass B)

getDetails(String a, String B)

【讨论】:

  • 正如我在帖子中提到的,在我现有的项目中,我们从多个地方调用 getDetails 并且 getDetails 方法很长,因此重载成本太高,会导致代码重复
【解决方案2】:

如果我理解你想要做什么,我通常会通过重载方法来实现它

getDetails(String a, Customclass B) {
    //lengthy calculation long method
    String status = getStatus(B)
    //lengthy calculation long method
}

getDetails(String a, String B) {
    String status = B
}

在重载中,您提供了 2 个或更多具有不同参数要求的方法。那么当你调用方法的时候,java会根据你传入的参数的类型来决定调用哪个方法。

【讨论】:

  • 正如我在帖子中提到的,在我现有的项目中,我们从多个地方调用 getDetails 并且 getDetails 方法很长,因此重载成本太高,会导致代码重复
  • @sgiowtswpjtwhhjtkw 您可以将一个重载委托给另一个,或者将公共代码提取到另一个私有方法中......
【解决方案3】:

重载可能不重复,通过将调用从一个委托给另一个:

getDetails(String a, Customclass B) {
    getDetails(a, getStatus(B));
}

getDetails(String a, String status) {
    // just keep this method
}

String getStatus(Customclass object) {
    // I guess you have this one already
} 

第2行的调用不是递归什么的,实际上是对第5行方法的调用。

【讨论】:

  • 没有人希望在重载方法中有代码重复。这就是为什么常见的方法是只让一个重载的方法做实际的工作(在你的情况下,一个接受状态字符串的方法),而让其他方法只翻译它们的参数并将它们转发给真正的方法
  • 如果您还有任何问题,请告诉我
  • 你对 String getStatus(Customclass object) 实现的建议{ // 我猜你已经有了这个 }
【解决方案4】:
interface ICustomInterface {
    //some code if needed
}

class StringWrapper implements ICustomInterface {
    private String value;

    public StringWrapper(String str) {
        this.value = str;
    }

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }
}

public class CustomClass implements ICustomInterface {
    //some code

    public static void getDetails(String a, ICustomInterface b) {
        if (b instanceof CustomClass) {
            System.out.println("Custom class");
            //some code
        } else if (b instanceof StringWrapper) {
            System.out.println(((StringWrapper) b).getValue());
            //some code
        }
    }


    public static void main(String[] args) {
        CustomClass customClass = new CustomClass();
        CustomClass.getDetails("first case", customClass);
        CustomClass.getDetails("second case", new StringWrapper("String"));
    }
}

【讨论】:

  • 我又在想那个对象 b,它位于顶层,我们不应该使用它
  • 然后将该 String 包装到某个类中,让它们都实现某个接口,这样您的参数将看起来像 (String a, ISomeInterface b),然后使用带有 instanceof 的条件。
猜你喜欢
  • 2012-06-02
  • 2016-09-18
  • 1970-01-01
  • 2016-09-23
  • 2012-05-28
  • 1970-01-01
  • 2012-04-12
  • 2016-01-25
相关资源
最近更新 更多