简体   繁体   中英

Method accepting two different types as parameter in Java

Requirement :-

In getDetails method, if object B of type Customclass is passed, then we are calling getStatus method which is taking Customclass as argument. Now, we need to make the argument which can take both string / customclass type,

    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

In my existing project, we are invoking getDetails from multiple places and getDetails is lengthy method, so overloading is too costly which will make code repetition

Please suggest some other ways

I have code something like similar below:-

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

Which I want to make it like below:-

   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
   }

create two method getdetails:

getDetails(Strig a, Customclass B)

getDetails(Strig a, String B)

If I understand what you want to do, I would normally achieve it with overloading the method

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
}

In overloading, you provide 2 or more methods that have different parameter requirements. Then when you call the method, java determines which method to call, based on the types of the parameters you pass in.

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"));
    }
}

Overloading might be possible without duplication , by delegating the call from one to the other:

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
} 

The call in line 2 is not recursion or anything, but is actually a call to the method of line 5.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM