设计一个名为MyTime类。该类包含: 1、表示时间的数据域hours,minse,second。 2、一个无参构造方法,该方法创建当前时间为0点0秒0时的对象。 3、一个带3个参数的构造方法,该方法创建一个带指定时、分、秒的MyTime对象。 4、数据域的set/get方法 5、重写toString()方法
时间: 2023-05-28 16:06:21 浏览: 169
public class MyTime {
private int hours;
private int minutes;
private int seconds;
public MyTime() {
this.hours = 0;
this.minutes = 0;
this.seconds = 0;
}
public MyTime(int hours, int minutes, int seconds) {
this.hours = hours;
this.minutes = minutes;
this.seconds = seconds;
}
public int getHours() {
return hours;
}
public void setHours(int hours) {
this.hours = hours;
}
public int getMinutes() {
return minutes;
}
public void setMinutes(int minutes) {
this.minutes = minutes;
}
public int getSeconds() {
return seconds;
}
public void setSeconds(int seconds) {
this.seconds = seconds;
}
@Override
public String toString() {
String hourString = String.format("%02d", hours);
String minuteString = String.format("%02d", minutes);
String secondString = String.format("%02d", seconds);
return hourString + ":" + minuteString + ":" + secondString;
}
}
阅读全文