前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >python面向对象小练习

python面向对象小练习

作者头像
流川疯
发布2019-01-18 16:35:23
5900
发布2019-01-18 16:35:23
举报
文章被收录于专栏:流川疯编写程序的艺术

就是几个动物,自动排列生成什么的

代码语言:javascript
复制
class Animal(object):
	def __init__(self,name,weight):
		self.name = name
		self.weight = weight
	def eat(self):
		self.weight +=1
	def speak(self):
		print ("i am a animal")
	def walk(self):
		print ("i am walking")	
	
class Dog(Animal):
	def __init__(self,name,weight):
		Animal.__init__(self,name,weight)
	def eat():
		self.weight +=1
	def speak(self):
		print ("i am a dog")
	def walk(self):
		print ("i am walking")	
class Duck(Animal):
	def __init__(self,name,weight):
		Animal.__init__(self,name,weight)
	def eat(self):
		self.weight +=1
	def speak(self):
		print ("i am a duck")
	def walk(self):
		print ("i am walking")	
class Cat(Animal):
	def __init__(self,name,weight):
		Animal.__init__(self,name,weight)
	def eat(self):
		self.weight +=1
	def speak(self):
		print ("i am a dog")
	def walk():
		print ("i am walking")	



#animal = Dog("Dog",24)

#animal.speak()

def reAnimals(zoo):
	
	string = "animal"
	for x in range(0,21):
		if x%3 ==0:
			animal = Dog(string+str(x),x+2)
		if x%3 ==1:
			animal = Duck(string+str(x),x)		
		if x%3 ==2:
			animal = Cat(string+str(x),x)
		zoo.append(animal)
	return zoo


#zoo = [item for item in animal if item.weight <= 10 and item.weight >= 0]

def filterAnimal(animal):
	zoo = []
	for x in range(0,len(animal)):
		if animal[x].weight<=10 and animal[x].weight>=0:
			zoo.append(animal[x])
	#animal.clear()
	#animal = zoo
	return zoo

animal = []
dongwu = []

dongwu = filterAnimal(reAnimals(dongwu))

for x in dongwu:
	x.speak()
	print (x.weight)



#print (animal[x].weight)

改版代码:

代码语言:javascript
复制
class Animal(object):
	def __init__(self,name,weight):
		self.name = name
		self.weight = weight
	def eat(self):
		self.weight +=1
	def fly(self):
		print ("i am a animal and i can fly")
	def jump(self):
		print ("i can jump ")	
	
class Tiger(Animal):
	def __init__(self,name,weight):
		Animal.__init__(self,name,weight)
	def eat():
		self.weight +=1
	def fly(self):
		print ("i am a Tiger and i cant fly")
	def jump(self):
		print ("i can jump ")		
class Bird(Animal):
	def __init__(self,name,weight):
		Animal.__init__(self,name,weight)
	def eat(self):
		self.weight +=1
	def fly(self):
		print ("i am a bird and i can fly")
	def jump(self):
		print ("i can jump ")	
class Snake(Animal):
	def __init__(self,name,weight):
		Animal.__init__(self,name,weight)
	def eat(self):
		self.weight +=1
	def fly(self):
		print ("i am a snake and i cant fly")
	def jump(self):
		print ("i cant jump ")	
        
container = []
dongwu = []

class Zoo(object):
    def filterAnimal(animal):
        container = []
        for x in range(0,len(animal)):
            if animal[x].weight<=10 and animal[x].weight>=0:
                container.append(animal[x])
        return container
    def reAnimals(container):
        string = "animal"   
        for x in range(0,21):
            if x%3 ==0:
                animal = Tiger(string+str(x),x+2)
            if x%3 ==1:
                animal = Bird(string+str(x),x)		
            if x%3 ==2:
                animal = Snake(string+str(x),x)
            container.append(animal)
        return container	
    def relax():
        dongwu = Zoo.filterAnimal(Zoo.reAnimals(container))
        for x in dongwu:
            x.fly()
            x.jump()
            

Zoo.relax()
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2015年04月24日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档