我刚刚开始掌握课程的窍门,所以我想我会尝试练习它制作一个简单的人口动态程序。我不太明白如何实现 super()。我正在阅读一些旧帖子和其他在线论坛的东西,但我什么也做不了。有人可以向我解释为什么吗?
class Females(object):
'''Female population settings. Defines the pregnancy rates, death rates,
and live births for a year.'''
def __init__(self,female):
super(Females,self).__init__()
self.female = female
self.live_birth
def __str__(self):
return 'Current female population: {0}'.format(str(self.female))
def death(self):
self.female_death_oldage=int((randint(0,50)*0.01)*female)
return self.female_death_oldage
def pregnancies(self):
self.female_pregnancies=int(((randint(0,100)*0.01)*female)*0.2)
return self.female_pregnancies
def live_birth(self):
self.live_births=int(self.female_pregnancies*((randint(0,100)*0.01)))
return self.live_births
def total_females(self):
self.next_female_generation = female - self.female_death_oldage
class Babies(Females):
def __init__(self):
super(Babies,self).__init__()
def babies_born(self):
self.little_girls = int(self.live_births*(randint(0,100)*0.01))
self.little_boys = (self.live_births - self.little_girls)
return self.little_boys,self.little_girls
if __name__=='__main__':
x=Males(male)
y=Females(female)
b=Babies()
print '%r males died from old age' % x.death()
print '%r females became pregnant' % y.pregnancies()
print 'There were %r live births' % y.live_births()
print b.babies_born()
当我尝试运行它(使用 Eclipse Juno)时,我得到了这个:
File "/Users/me/Documents/Coding/Population/Population.py", line 78, in <module>
b=Babies()
File "/Users/me/Documents/Coding/Population/Population.py", line 56, in __init__
super(Babies,self).__init__()
TypeError: __init__() takes exactly 2 arguments (1 given)