python class 内的 next(),last(),set() 写法卡了一段时间不太知道怎么写,我有一个class 里面需要有next,last,与set()答案要这样:我搞不定- ->d= MyDate(22,10,2012)>s= Schedule(d,usage = "School",owner = "CaCaEgg") # d wil
来源:学生作业帮助网 编辑:六六作业网 时间:2024/11/16 23:42:34
python class 内的 next(),last(),set() 写法卡了一段时间不太知道怎么写,我有一个class 里面需要有next,last,与set()答案要这样:我搞不定- ->d= MyDate(22,10,2012)>s= Schedule(d,usage = "School",owner = "CaCaEgg") # d wil
python class 内的 next(),last(),set() 写法
卡了一段时间不太知道怎么写,
我有一个class 里面需要有next,last,与set()
答案要这样:我搞不定- -
>d= MyDate(22,10,2012)
>s= Schedule(d,usage = "School",owner = "CaCaEgg") # d will set as current
>d1= MyDate(29,11,2012)
>d2= MyDate(16,9,2012)
>d3= MyDate(25,9,2012)
>s.add(d1)
>s.add(d2)
>s.add(d3)
>prints.next()
29/11/2012
>prints.last()
25/9/2012
>d= MyDate(20,9,2012)
>s.set(d)
>prints.next()
25/9/2012
>prints.last()
16/9/2012
我的程式码:class Schedule(calender):
def __init__(self,ddate,usage,owner):
self.usage = usage
self.owner = owner
self.current = []
self.current.append(str(ddate))
def __str__(self):
return str(self.current)
def add(self,x):
self.current.append(str(x))
def daylist(self):
return self.current
题目是这样:
请利用继承calender 来解决,请撰写一个Schedule的CLASS,拥有以下members method
members:
current,a MyDateclass
functions:
set(MyDate):set MyDateobject to self.current
next():return the first MyDateobject in daylistafter self.current
last():return the last MyDateobject in daylistbefore self.current
python class 内的 next(),last(),set() 写法卡了一段时间不太知道怎么写,我有一个class 里面需要有next,last,与set()答案要这样:我搞不定- ->d= MyDate(22,10,2012)>s= Schedule(d,usage = "School",owner = "CaCaEgg") # d wil
class Schedule(calender):
def __init__(self, date, usage, owner):
self.usage = usage
self.owner = owner
# 保存所有的日期
self._list = [date, ]
self.current = date
def add(self, date):
self._list.append(date)
# 假定date对象是支持比较的
self._list.sort()
def set(self, date):
if date not in self._list:
self.add(date)
self.current = date
def next(self):
index = self._list.index(self.current) + 1
if index >= len(self._list):
print "no next"
else:
return self._list[index]
last类似,略