定义一个时钟类—clock,包括三个int型的成员变量,分别表示时、分、秒,写两个构造方法,一个带参数,一个不带参数,有一个成员方法—show(),用于显示当前的时间。编写主类并实例化3个对象进行测试
时间: 2023-05-31 10:20:02 浏览: 1552
### 回答1:
class Clock:
def __init__(self, hour=, minute=, second=):
self.hour = hour
self.minute = minute
self.second = second
def show(self):
print(f"{self.hour:02d}:{self.minute:02d}:{self.second:02d}")
clock1 = Clock()
clock2 = Clock(12, 30, 45)
clock3 = Clock(23, 59, 59)
clock1.show()
clock2.show()
clock3.show()
### 回答2:
时钟类是一个用于表示时间的类,其成员变量包括三个int型数据,分别表示时、分、秒。时钟类应该具有两个构造方法,一个是带参数的,可以传入时、分、秒的值,另一个是不带参数的,构造一个默认的时间为0:0:0的时钟对象。此外,时钟类还应该拥有一个成员方法show(),用于显示当前的时间。
在构造带参数的构造方法时,需要对传入的时、分、秒的值做一些限制,确保时间值的正确性。例如,时的范围应该在0到23之间,分和秒的范围应该在0到59之间。如果传入的值不满足这些条件,应该抛出异常或者在构造方法中做出更合适的处理。
下面是一个时钟类的实现:
class Clock {
private int hour;
private int minute;
private int second;
public Clock() {
this(0, 0, 0);
}
public Clock(int hour, int minute, int second) {
setHour(hour);
setMinute(minute);
setSecond(second);
}
public void show() {
System.out.printf("%02d:%02d:%02d\n", hour, minute, second);
}
public int getHour() {
return hour;
}
public void setHour(int hour) {
if (hour < 0 || hour > 23) {
throw new IllegalArgumentException("Hour must be between 0 and 23");
}
this.hour = hour;
}
public int getMinute() {
return minute;
}
public void setMinute(int minute) {
if (minute < 0 || minute > 59) {
throw new IllegalArgumentException("Minute must be between 0 and 59");
}
this.minute = minute;
}
public int getSecond() {
return second;
}
public void setSecond(int second) {
if (second < 0 || second > 59) {
throw new IllegalArgumentException("Second must be between 0 and 59");
}
this.second = second;
}
}
在主类中,可以实例化3个时钟对象,并分别测试其show()方法是否正确输出了当前的时间:
public class Main {
public static void main(String[] args) {
Clock clock1 = new Clock();
Clock clock2 = new Clock(9, 30, 0);
Clock clock3 = new Clock(23, 59, 59);
clock1.show();
clock2.show();
clock3.show();
}
}
输出结果如下:
00:00:00
09:30:00
23:59:59
### 回答3:
时钟类(Clock)是一个非常常见的类,它用于表示时间。这个类包括三个 int 类型的成员变量,分别表示小时、分钟和秒钟。为了创建一个时钟类,我们需要编写两个构造方法,它们分别是带参数的构造方法和不带参数的构造方法。
带参数的构造方法是用来初始化时钟对象的变量的。它接收三个整型参数,分别表示小时、分钟和秒钟。在构造方法中,我们可以使用 this 关键字来引用对象的当前实例,来给对象的成员变量赋值。
另一个构造方法是不带参数的构造方法,它用来初始化时钟对象的变量,也可以用来创建一个默认的时钟对象。这个构造方法会将小时、分钟和秒钟的初始值设置为 0。
show() 方法是用来显示当前时间。在方法中,我们可以使用 System.out.println() 输出当前的小时、分钟和秒钟。
下面是时钟类的代码:
```
class Clock {
private int hour;
private int minute;
private int second;
public Clock() {
this.hour = 0;
this.minute = 0;
this.second = 0;
}
public Clock(int hour, int minute, int second) {
this.hour = hour;
this.minute = minute;
this.second = second;
}
public void show() {
System.out.println("The current time is: " + this.hour + ":" + this.minute + ":" + this.second);
}
}
```
接下来让我们来创建一个主类,并实例化三个时钟对象进行测试。
```
public class Main {
public static void main(String[] args) {
Clock clock1 = new Clock();
Clock clock2 = new Clock(7, 30, 45);
Clock clock3 = new Clock(12, 0, 0);
clock1.show();
clock2.show();
clock3.show();
}
}
```
运行程序后,会输出以下内容:
```
The current time is: 0:0:0
The current time is: 7:30:45
The current time is: 12:0:0
```
这表明我们的时钟类和主程序代码没有问题,能够正常工作。
阅读全文
相关推荐


















