美文网首页
Python从新手到大师——08:模拟日期时间

Python从新手到大师——08:模拟日期时间

作者: 远航天下 | 来源:发表于2018-08-23 14:54 被阅读0次

代码一

#! /usr/bin/env python
"""
@Time     : 2018/8/23 11:28
@Author   : Damao
@Site     : Life is short. I use python.
@File     : test1.py
@Software : PyCharm

"""
"""模拟日期时间"""

import  time
import os


class DateAndTime(object):
    def __init__(self,**alldatetime):
        if ('year' in alldatetime and 'month'in alldatetime
                and 'today'in alldatetime and 'hour' in alldatetime
                and 'minute' in alldatetime and 'second' in alldatetime):
            self._year = alldatetime['year']
            self._month = alldatetime['month']
            self._today = alldatetime['today']
            self._hour = alldatetime['hour']
            self._minute = alldatetime['minute']
            self._second = alldatetime['second']
        else:
            date_time = time.localtime(time.time())
            self._year = date_time.tm_year
            self._month = date_time.tm_mon
            self._today = date_time.tm_mday
            self._hour = date_time.tm_hour
            self._minute = date_time.tm_min
            self._second = date_time.tm_sec

    def go_time(self):
        self._second += 1
        if self._second == 60:
            self._second = 0
            self._minute += 1
            if self._minute == 60:
                self._minute = 0
                self._hour += 1
                if self._hour == 24:
                    self._hour = 0

    def show_time(self):
        return ("{a}年{b}月{c}日-{d}时{e}分{f}秒"
                .format(a=self._year,b=self._month,c=self._today,
                        d=self._hour,e=self._minute,f=self._second))


if __name__ == '__main__':
    test = DateAndTime()
    while True:
        # os.system("cls")
        print("现在日期时间是:",test.show_time().center(50,' '))
        time.sleep(1)
        test.go_time()

代码二

#! /usr/bin/env python
"""
@Time     : 2018/8/23 14:35
@Author   : Damao
@Site     : Life is short. I use python.
@File     : test2.py
@Software : PyCharm

"""
"""另一种创建类的方法"""


def name(self,name):
    self.name = name

def read(self,book):
    print("{a}正在读一本叫'{b}'的书".format(a=self.name,b=book))


if __name__ == '__main__':
    Do = type('ToDo', (object,), dict(__init__=name, to_read=read))
    abc = Do("damao")
    abc.to_read("AV")

相关文章

网友评论

      本文标题:Python从新手到大师——08:模拟日期时间

      本文链接:https://www.haomeiwen.com/subject/skxmiftx.html