I have a method that takes a List and another class object as its arguments. I want to iterate through the list based on the class object that I am passing into the method. The class has three different constructors. One of them takes Date objects, the other takes strings, and the third takes ints. In my method, I would like to do different things depending on which constructor is being used for the class object being used. Is there a way to do this? Is there a way I can do an if else statement that checks for the type of class object (based on constructor) being used?
For CodeWarrior: Here are constructor examples:
public DateRange(Date date1, Date date2){
}
public DateRange(String string1, String string2){
}
public DateRange(int month, int year){
}
Then say I have a method like this:
public static List<Schedule> getSchedule(List<Schedule> schedules, DateRange dateRange) {
List<Schedule> schedules = new ArrayList<Schedule>();
for (Schedule scheduleTime : schedules){
if
I would like to set up the method to design the if statements to somehow check which type of the DateRange had been used, and do different things based on that.
I think this is what you are looking for.
Modify your DateRange class so it can tell you how it was created:
Here is the calling/checking syntax:
Another option requiring further modification of your
DateRangeclass would be to use inheritance to make a family ofDateRangeclasses based on the constructor type. The baseDateRangeclass would have an abstract method akin todoSomething()which would be provided by each of the inheritedDateRangeDate,DateRangeString, andDateRangeIntclasses. Then, you might not even have to process through the if tree in your iteration.I hope this helps!