需求:-

在getDetails方法中,如果传递了Customclass类型的对象B,则我们将调用以Customclass作为参数的getStatus方法。
现在,我们需要使参数既可以采用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
   }

最佳答案

创建两个方法getdetails:

getDetails(Strig a,Customclass B)

getDetails(字符串a,字符串B)

关于java - Java中接受两种不同类型作为参数的方法,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/60396306/

10-10 02:37