首页 > 代码库 > 一个有意义的Day类

一个有意义的Day类

早晨去单位的路上听到电台里在说“Everyday is a new chance to change your life”,正好最近在学Python类的使用方法,于是我编了一个关于Day的类,以供参考。

 1 class Day(object):
 2 
 3     def __init__(self, when):
 4         self.when = when
 5         
 6     def result(self):
 7         if self.when == "today":
 8             print self.when.title(),"is in my hands!"
 9         elif self.when == "tomorrow":
10             print self.when.title(),"is a new chance to change your life!"
11         elif self.when == "yesterday":
12             print self.when.title(),"comes true."
13         else:
14             print "It‘s a good day!"
15             
16 my_day = Day("today")
17 your_day = Day("tomorrow")
18 
19 my_day.result()
20 your_day.result()

当然,结果是:

Today is in my hands!
Tomorrow is a new chance to change your life!

 

一个有意义的Day类