简体   繁体   中英

Constructor overload

This is my class:

public class DateTime {

private int hours;
private int minutes;
private int seconds;
private int day;
private int month;
private int year;



public DateTime(int hours, int minutes, int seconds) {


    Calendar c = Calendar.getInstance();
    int day = c.get(Calendar.DAY_OF_MONTH);
    int month = c.get(Calendar.MONTH);
    int year = c.get(Calendar.YEAR);

    this(hours,minutes,seconds,day,month,year); //Error: call to this must be first statement in constructor java

}


public DateTime(int hours, int minutes, int seconds, int day, int month, int year) {

    this.hours = hours;
    this.minutes = minutes;
    this.seconds = seconds;
    this.day = day;
    this.month = month;
    this.year = year;
}
}

I wish to overload the constructor with this logic,
But I get error:
"call to this must be first statement in constructor java" ,
can you please suggest a solution?

As 'this()' should be the first one to be called by constructor. Then there is only solution. You can use like this:

public class DateTime {

private int hours;
private int minutes;
private int seconds;
private int day;
private int month;
private int year;



public DateTime(int hours, int minutes, int seconds) {

Calendar c = Calendar.getInstance();
this.day = c.get(Calendar.DAY_OF_MONTH);
this.month = c.get(Calendar.MONTH);
this.year = c.get(Calendar.YEAR);
this.hours = hours;
this.minutes = minutes;
this.seconds = seconds;

}
}

Now i think this will work fine.

There are several solutions...

0) inline the constructor call and set hours minutes and seconds directly, it's not worth the trouble

1) use an intermediary constructor

public DateTime(int hours, int minutes, int seconds) {
    this(hours, minutes, seconds, Calendar.getInstance());
}
private DateTime(int hours, int minutes, int seconds, Calendar c) {
    this(hours,minutes,seconds,c.get(Calendar.DAY_OF_MONTH),c.get(Calendar.MONTH),c.get(Calendar.YEAR));
}

2) use a factory method instead of constructor

public static DateTime newDateTime(int hours, int minutes, int seconds) {
    Calendar c = Calendar.getInstance();
    int day = c.get(Calendar.DAY_OF_MONTH);
    int month = c.get(Calendar.MONTH);
    int year = c.get(Calendar.YEAR);
    return new DateTime(hours,minutes,seconds,day,month,year);
}

3) invert the dependency (day month year get set twice)

public DateTime(int hours, int minutes, int seconds, int day, int month, int year) {
    this(hours, minutes, seconds);
    this.day = day;
    this.month = month;
    this.year = year;
}

4) delegate to method instead of constructor

public DateTime(int hours, int minutes, int seconds) {
    Calendar c = Calendar.getInstance();
    int day = c.get(Calendar.DAY_OF_MONTH);
    int month = c.get(Calendar.MONTH);
    int year = c.get(Calendar.YEAR);
    set(hours,minutes,seconds,day,month,year);
}
public DateTime(int hours, int minutes, int seconds, int day, int month, int year) {
    set(hours,minutes,seconds,day,month,year);
}
private final void set(int hours, int minutes, int seconds, int day, int month, int year) { ... }

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