#-*-coding:utf-8-*- ''' Created on 2014年7月7日 @author: xyt ''' '''尝试抽象一个牛群''' class Cows(): def __init__(self,age): self.age=age self.count_age_1=0#1岁奶牛 self.count_age_2=0#2岁奶牛 self.count_age_3=0#3岁奶牛 self.count_age_all=1#成年奶牛 def countCow(self): for i in range(1,self.age): self.count_age_all=self.count_age_3+self.count_age_all self.count_age_3=self.count_age_2 self.count_age_2=self.count_age_1# self.count_age_1=self.count_age_all#有多少成年奶牛 过一年就会有多少1岁奶牛 print "1岁奶牛:"+str(self.count_age_1) print "2岁奶牛:"+str(self.count_age_2) print "3岁奶牛:"+str(self.count_age_3) print "成年奶牛:"+str(self.count_age_all) print "第"+str(i)+"年总共:"+str(self.count_age_1+self.count_age_2+self.count_age_3+self.count_age_all) return self.count_age_1+self.count_age_2+self.count_age_3+self.count_age_all '''How many Cows do you have''' def countCow(age): pass if __name__=='__main__': sum=Cows(7) print sum.countCow()