比较两个日期。类时间继承类日期。
创始人
2024-12-14 10:30:40
0

要比较两个日期,可以创建一个继承自日期类的时间类。下面是一个示例代码:

from datetime import datetime

class Date:
    def __init__(self, year, month, day):
        self.year = year
        self.month = month
        self.day = day
        
    def __lt__(self, other):
        if isinstance(other, Date):
            return datetime(self.year, self.month, self.day) < datetime(other.year, other.month, other.day)
        else:
            raise TypeError("Cannot compare Date with non-Date object.")
    
    def __gt__(self, other):
        if isinstance(other, Date):
            return datetime(self.year, self.month, self.day) > datetime(other.year, other.month, other.day)
        else:
            raise TypeError("Cannot compare Date with non-Date object.")
    
    def __eq__(self, other):
        if isinstance(other, Date):
            return datetime(self.year, self.month, self.day) == datetime(other.year, other.month, other.day)
        else:
            raise TypeError("Cannot compare Date with non-Date object.")

class Time(Date):
    def __init__(self, year, month, day, hour, minute, second):
        super().__init__(year, month, day)
        self.hour = hour
        self.minute = minute
        self.second = second
        
    def __lt__(self, other):
        if isinstance(other, Time):
            return datetime(self.year, self.month, self.day, self.hour, self.minute, self.second) < datetime(other.year, other.month, other.day, other.hour, other.minute, other.second)
        else:
            raise TypeError("Cannot compare Time with non-Time object.")
    
    def __gt__(self, other):
        if isinstance(other, Time):
            return datetime(self.year, self.month, self.day, self.hour, self.minute, self.second) > datetime(other.year, other.month, other.day, other.hour, other.minute, other.second)
        else:
            raise TypeError("Cannot compare Time with non-Time object.")
    
    def __eq__(self, other):
        if isinstance(other, Time):
            return datetime(self.year, self.month, self.day, self.hour, self.minute, self.second) == datetime(other.year, other.month, other.day, other.hour, other.minute, other.second)
        else:
            raise TypeError("Cannot compare Time with non-Time object.")

# 示例用法
date1 = Date(2021, 1, 1)
date2 = Date(2022, 1, 1)
time1 = Time(2021, 1, 1, 12, 0, 0)
time2 = Time(2021, 1, 1, 15, 0, 0)

print(date1 < date2)  # 输出: True
print(date1 == date2)  # 输出: False
print(time1 < time2)  # 输出: True
print(time1 == time2)  # 输出: False

在上面的示例中,我们创建了一个Date类来表示日期,然后创建了一个Time类来表示日期和时间。Time类继承自Date类,因此可以直接使用Date类中定义的比较方法。

在比较方法中,我们使用datetime模块的datetime类来进行日期和时间的比较。datetime类提供了比较运算符来比较两个日期和时间的大小关系。

注意,在比较方法中我们还添加了类型检查和错误处理,如果比较的对象不是DateTime类型的,会抛出TypeError异常。

最后,我们展示了如何使用这些类来比较两个日期和时间的大小关系。

相关内容

热门资讯

自建房九分钟!wepoke保险... 自建房九分钟!wepoke保险云扑克确实真的有挂(其实真的有挂)-百度知乎1.云扑克确实真的有挂 a...
漏洞3分钟!德州扑克微扑克俱乐... 漏洞3分钟!德州扑克微扑克俱乐部德扑ai智能机器人(一贯真的有挂)-今日头条所有人都在同一条线上,像...
输赢两分钟!wpk线上代打wp... 您好,wpk数据工具这款游戏可以开挂的,确实是有挂的,需要了解加微【487309276】很多玩家在这...
挂6分钟!wopoker有外挂... 挂6分钟!wopoker有外挂智星德州菠萝偷偷看功能(果真真的有挂)-微博客户端;1、智星德州菠萝偷...
神器2分钟!wpk德州ai机器... 神器2分钟!wpk德州ai机器人gg扑克正规(原来真的有挂)-知乎;1、用户打开应用后不用登录就可以...
胜率九分钟!德扑之星有作弊wo... 胜率九分钟!德扑之星有作弊wopoker透明挂(原来真的有挂)-知乎1、下载好德扑之星有作弊辅助软件...
安卓版五分钟!微扑克机制技巧f... 安卓版五分钟!微扑克机制技巧fishpoker app辅助(好像真的有挂)-小红书1、下载好微扑克机...
辅助6分钟!微扑克软件开发定制... 辅助6分钟!微扑克软件开发定制app来玩德州app苹果下载(总是真的有挂)-小红书1、许多玩家不知道...
智能四分钟!pokerx智能软... 智能四分钟!pokerx智能软件wepower软件靠谱(原来真的有挂)-小红书暗藏猫腻,小编详细说明...
猫腻十分钟!微扑克真的有外挂的... 猫腻十分钟!微扑克真的有外挂的来玩德州有外挂(原来真的有挂)-哔哩哔哩1、进入游戏-大厅左侧-新手福...