Python を使用して時間クラスを作成し、時間の加算・減算操作をサポート
という名前のTimeクラスを作成します。このクラスは時間(時、分、秒)を表すことができ、時間の加算・減算操作をサポートします。私たちは__add__と__sub__メソッドを実装して、時間の加算・減算操作をサポートします。
例
class Time:
def __init__(self, hours, minutes, seconds):
self.hours = hours
self.minutes = minutes
self.seconds = seconds
self.normalize()
def normalize(self):
extra_minutes, self.seconds = divmod(self.seconds, 60)
self.minutes += extra_minutes
extra_hours, self.minutes = divmod(self.minutes, 60)
self.hours += extra_hours
def __add__(self, other):
total_seconds = self.hours * 3600 + self.minutes * 60 + self.seconds
total_seconds += other.hours * 3600 + other.minutes * 60 + other.seconds
hours, remainder = divmod(total_seconds, 3600)
minutes, seconds = divmod(remainder, 60)
return Time(hours, minutes, seconds)
def __sub__(self, other):
total_seconds = self.hours * 3600 + self.minutes * 60 + self.seconds
total_seconds -= other.hours * 3600 + other.minutes * 60 + other.seconds
hours, remainder = divmod(total_seconds, 3600)
minutes, seconds = divmod(remainder, 60)
return Time(hours, minutes, seconds)
def __str__(self):
return f"{self.hours:02}:{self.minutes:02}:{self.seconds:02}"
# 使用例
time1 = Time(2, 30, 45)
time2 = Time(1, 15, 20)
print("Time 1:", time1)
print("Time 2:", time2)
time_sum = time1 + time2
print("Time 1 + Time 2:", time_sum)
time_diff = time1 - time2
print("Time 1 - Time 2:", time_diff)
def __init__(self, hours, minutes, seconds):
self.hours = hours
self.minutes = minutes
self.seconds = seconds
self.normalize()
def normalize(self):
extra_minutes, self.seconds = divmod(self.seconds, 60)
self.minutes += extra_minutes
extra_hours, self.minutes = divmod(self.minutes, 60)
self.hours += extra_hours
def __add__(self, other):
total_seconds = self.hours * 3600 + self.minutes * 60 + self.seconds
total_seconds += other.hours * 3600 + other.minutes * 60 + other.seconds
hours, remainder = divmod(total_seconds, 3600)
minutes, seconds = divmod(remainder, 60)
return Time(hours, minutes, seconds)
def __sub__(self, other):
total_seconds = self.hours * 3600 + self.minutes * 60 + self.seconds
total_seconds -= other.hours * 3600 + other.minutes * 60 + other.seconds
hours, remainder = divmod(total_seconds, 3600)
minutes, seconds = divmod(remainder, 60)
return Time(hours, minutes, seconds)
def __str__(self):
return f"{self.hours:02}:{self.minutes:02}:{self.seconds:02}"
# 使用例
time1 = Time(2, 30, 45)
time2 = Time(1, 15, 20)
print("Time 1:", time1)
print("Time 2:", time2)
time_sum = time1 + time2
print("Time 1 + Time 2:", time_sum)
time_diff = time1 - time2
print("Time 1 - Time 2:", time_diff)
コード解説:
__init__メソッド:初期化Timeクラスのインスタンスは、時、分、秒をパラメータとして受け取り、normalizeメソッドを呼び出して、時間の形式が正しいことを保証します。normalizeメソッド:秒と分を標準形式に変換し、秒が59を超えず、分が59を超えないことを保証します。__add__メソッド:2つのTimeオブジェクトの加算操作。時間を秒数に変換して加算し、再び時、分、秒に変換します。__sub__メソッド:2つのTimeオブジェクトの減算操作。時間を秒数に変換して減算し、再び時、分、秒に変換します。__str__メソッド:フォーマットされた時間文字列を返し、時、分、秒がすべて2桁になることを保証します。
出力結果:
Time 1: 02:30:45 Time 2: 01:15:20 Time 1 + Time 2: 03:46:05 Time 1 - Time 2: 01:15:25その他の拡張
Python3 例