简体   繁体   中英

constructors and compilation

Two things:

  1. This is a section of a program that I wrote, but it is not running.

  2. Generally, the mission that I got includes defining 2 constructors, and I do not understand how it is possible to a class to work with 2 constructors, logically. I think I managed to create the basic constructor, but I do not know how to create it (and why?).

The 2nd constructor (the constructor that is displayed in the code between /* and */) needs to receive another time and to copy its values.

Thanks for any help!

==================================================================================

public class Time1 {

private int _hour,
            _minute,
            _second;

public Time1 (int h, int m, int s) {
    if (hourIsValid (h))
        _hour = h;
    else _hour = 0;

    if (minuteIsValid (m))
        _minute = m;
    else minute = 0;

    if (secondIsValid (s))
        _second = s;
    else _second = 0;
}

boolean hourIsValid (h) {
    return (h <= 23 && h >= 0);
}

boolean minuteIsValid (m) {
    return (m <= 59 && m >= 0);
}

boolean secondIsValid (s) {
    return (s <= 59 && s >= 0);
}

/*  public Time1 (Time1 other) {...




}
*/

int getHour() {
    return _hour;
}

int getMinute() {
    return _minute;
}

int getSecond() {
    return _second;
}

void setHour (int num) {
    if (num >= 0 && num <= 23)
        _hour = num;
}

void setMinute (int num) {
    if (num >= 0 && num <=59)
        _minute = num;
}

void setSecond (int num) {
    if (num >= 0 && num <=59)
        _second = num;
}

String toString () {
    String strH,
           strM,
           strS;
    if (_hour < 10)
        strH = "0" + _hour + ":";
    else strH = _hour + ":";

    if (_minute < 10)
        strM = "0" + _minute + ":";
    else strM = _minute + ":";

    if (_second < 10)
        strS = "0" + _second;
    else strS = _second;

    return (strH + strM + strS);
}
    /*if (_hour < 10)
        System.out.print("0" + _hour + ":");
    else System.out.print(_hour + ":");

    if (_minute < 10)
        System.out.print("0" + _minute + ":");
    else System.out.print(_minute + ":");

    if (_second < 10)
        System.out.print("0" + _second);
    else System.out.print(_second); 
}
*/

 boolean equals (Time1 other) {
     return ((_hour == other._hour) && (_minute == other._minute) && (_second == other._second));
 }

 boolean before (Time1 other) {
     return ((_hour < other._hour) || 
             ((_hour == other._hour) && (_minute < other._minute)) || 
             ((_hour == other._hour) && (_minute == other._minute) && (_second < other._second)));
 }

 boolean after (Time1 other) {
     return other.before(this);
 }

 int difference (Time1 other) {
     int obSec, otherSec;

     obSec = ((_hour * 60 * 60) + (_minute * 60) + _second);
     otherSec = ( (other._hour * 60 * 60) + (other._minute * 60) + other._second);

     return (obSec - otherSec);
 }

}

It's called overloading. You can define methods with the same name as long as they have different parameters, but share return type (or return void, or in the case of constructors, lack return type). The compiler will know what method to use based on the parameters used to call it.

In this case I'd guess what you want is something like:

public Time1 (Time1 other) {
  this._hour = other.getHour();
  this._minute = other.getMinute();
  this._second = other.getSecond();
}

Another option would be to chain the constructors by using the this keyword like this:

public Time1 (Time1 other) {
  this(other.getHour(), other.getMinute(), other.getSecond());
}

As you want to copy the variables from the Time1-object you pass to the constructor. Of course you should add error checking etcetera, but I hope you get the general idea.

Oracle have some decent tutorials that you might want to look into.

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