python - Date 类 - 计算调用它的 Date 对象的星期几的方法

标签 python class oop object methods

我构建了一个 Date 类,它具有明天方法、add_n_days 方法、equals 方法、before 和 after 方法以及 Difference 方法。我想要弄清楚的是如何使用我的差异方法来创建一个新方法,在该方法中我可以计算出 Date 对象所在的星期几。

 # A class to represent calendar dates
 #

class Date:
""" A class that stores and manipulates dates,
    represented by a day, month, and year.
"""

# The constructor for the Date class.
def __init__(self, new_month, new_day, new_year):
    """ The constructor for objects of type Date. """
    self.month = new_month
    self.day = new_day
    self.year = new_year

# The function for the Date class that returns a Date
# object in a string representation.
def __repr__(self):
    """ This method returns a string representation for the
        object of type Date that it is called on (named self).

        ** Note that this _can_ be called explicitly, but
          it more often is used implicitly via printing or evaluating.
    """
    s = '%02d/%02d/%04d' % (self.month, self.day, self.year)
    return s

def is_leap_year(self):
    """ Returns True if the called object is
        in a leap year. Otherwise, returns False.
    """
    if self.year % 400 == 0:
        return True
    elif self.year % 100 == 0:
        return False
    elif self.year % 4 == 0:
        return True
    return False

def copy(self):
    """ Returns a new object with the same month, day, year
        as the called object (self).
    """
    new_date = Date(self.month, self.day, self.year)
    return new_date

def is_before(self,d2):
    if self.year<d2.year:
        return true 
    if self.month<d1.month and self.year==d2.year:
        return True
    if self.day < d2.day and d2.month == self.month and self.year ==d2.year:
        return true
    return False

def is_after(self,d2): 
    return d2.isbefore(self)


def diff(self,d2):
    dcopy=self.copy()
    difference=0
    if dcopy.isbefore(d2) == True:
        while dcopy.isBefore(d)==true:
        dcopy.tomorrow()
        difference-=1
    else:
        while dcopy.isafter(d2):
            dcopy.yesterday()
            difference +=1

    return difference


def diff(self,d2):
    dcopy=self.copy()
    difference=0
    while dcopy.isbefore(d2):
        dcopy.tomorrow()
        difference-=1
    while dcopy.isafter(d2):
        dcopy.yesterday()
        difference+=1

我正在考虑实现它的方式,但并不真正理解如何具体建立它是有一个已知的日期,一个包含工作日的列表,在尝试计算星期几时使用模数运算符 - %=7,和上面的diff(差值)方法一样。

解决这个问题的最佳方法是什么,我将不胜感激任何帮助和解释 - 我是 python 和 OOP 的新手。

谢谢。

day_of_week_names = ['星期一', '星期二', '星期三', '星期四', '星期五'、'星期六'、'星期日']

shell 中的输出,我认为应该是这样的

>>> d = Date(4, 4, 2016)
>>> d.day_of_week()
'Monday'
>>> Date(1, 1, 2100).day_of_week()
'Friday'
>>> Date(7, 4, 1776).day_of_week()
'Thursday'

最佳答案

如果我告诉你 2000 年 1 月 1 日是星期一,然后问你 50 天后是星期几,你就很容易猜出来了。

它只是 50 % 7,即 1。因此,我们知道我们正在查看的星期几是我们知道的那一天过去的一天。

因此 50 天后将是星期二,或者星期一过去 1 天。

由于您可以找出任何给定日期和所谓的锚定日期(如我上面给出的 1/1/2000 示例)之间有多少天,因此您可以计算任何日期是星期几。

关于python - Date 类 - 计算调用它的 Date 对象的星期几的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37953857/

相关文章:

python - 寻求有关安装 numpy 扩展的帮助

python - 我如何使用 setuptools 或 distutils 来分发脚本而不是 Python 包?

python - 登录类(class) - Python

c++ - 在 C++ 中访问成员对象的方法

language-agnostic - 对接口(interface)编程,通用性有多通用?

c# - 传递一个对象,继承它或使用直接方法

python - matplotlib 中的锯齿线样式?

Python list 不是同一个引用

ios - Swift iOS - 如何动态地将属性添加到现有类然后访问它们

java - 如何在 Java 中删除重复的列表遍历