0

我有一个将 List 和另一个类对象作为参数的方法。我想根据传递给方法的类对象遍历列表。该类具有三个不同的构造函数。其中一个接受 Date 对象,另一个接受字符串,第三个接受整数。在我的方法中,我想根据正在使用的类对象使用哪个构造函数来做不同的事情。有没有办法做到这一点?有没有办法我可以做一个 if else 语句来检查正在使用的类对象的类型(基于构造函数)?

对于 CodeWarrior:以下是构造函数示例:

    public DateRange(Date date1, Date date2){

}

public DateRange(String string1, String string2){

}

public DateRange(int month, int year){

}

然后说我有这样的方法:

public static List<Schedule> getSchedule(List<Schedule> schedules, DateRange dateRange) {
    List<Schedule> schedules = new ArrayList<Schedule>();
    for (Schedule scheduleTime : schedules){
        if

我想设置方法来设计 if 语句以某种方式检查使用了哪种类型的 DateRange,并基于此做不同的事情。

4

3 回答 3

1

我想这就是你要找的。

修改您的 DateRange 类,以便它可以告诉您它是如何创建的:

public class DateRange {
    private final Object obj1;
    private final Object obj2;
    public DateRange(Date date1, Date date2) {
        obj1 = date1;
        obj2 = date2;
    }
    public DateRange(String string1, String string2) {
        obj1 = string1;
        obj2 = string2;
    }
    public DateRange(int month, int year) {
        obj1 = Integer.valueOf(month);
        obj2 = Integer.valueOf(year);
    }
    public boolean isDate() { return obj1 instanceof Date; }
    public boolean isString() { return obj1 instanceof String; }
    public boolean isInt() { return obj1 instanceof Integer; }
}

这是调用/检查语法:

public static List<Schedule> getSchedule(List<Schedule> schedules, DateRange dateRange) {
    for (Schedule scheduleTime : schedules) {
        if(dateRange.isDate()) {
            // Do something based on the Date constructor.
        } else if(dateRange.isString()) {
            // Do something based on the String constructor.
        } else if(dateRange.isInt()) {
            // Do something based on the int constructor.
        }
    }
    return schedules;
}

需要进一步修改DateRange类的另一个选项是使用继承来创建DateRange基于构造函数类型的类族。基DateRange类将有一个抽象方法,类似于doSomething()将由每个继承DateRangeDateDateRangeString、 和DateRangeInt类提供。然后,您甚至可能不必在迭代中处理 if 树。

我希望这有帮助!

于 2013-02-13T07:07:34.570 回答
0

我不确定您是否可以获得用于实例化对象的构造函数类型...但是您可以做的是在每种类型的构造函数中设置一个特定参数,您稍后将检查。例如,您可以为每个构造函数设置一个布尔变量(最初均为 false)。如果调用了某个构造函数,请将其各自的变量设置为 true。或者,将整数字段设置为每个构造函数的特定数字,然后检查该字段的值。

于 2013-02-13T06:32:04.210 回答
0

JVM 不会存储用于实例化对象的构造函数版本。这是您必须以某种方式存储并相应编码的一些信息。对于您发布的示例,试试这个

//DateRange.java

public class DateRange{

    private int constructorType;

    public DateRange(Date date1, Date date2){
         constructorType = 1;
    }

    public DateRange(String string1, String string2){
         constructorType = 2;
    }

    public DateRange(int month, int year){
        constructorType = 3;
    }

    public int getConstructorType(){
          return constructorType;
    }
}

这是 getSchedule() 函数:

    public static List<Schedule> getSchedule(List<Schedule> schedules, DateRange dateRange) {
    List<Schedule> schedules = new ArrayList<Schedule>();
    for (Schedule scheduleTime : schedules){

        //use switch instead of if
        switch(dateRange.getConstructorType()){
           case 1: System.out.println("First constructor was called"); break;
           case 2: System.out.println("Second constructor was called"); break;
           case 3: System.out.println("Third constructor was called"); break;
           default: System.err.println("Something is wrong. Unknown constructor type"); break;
        }
    }
}
于 2013-02-13T07:08:08.113 回答