python面向对象小练习

简介: 就是几个动物,自动排列生成什么的 class Animal(object): def __init__(self,name,weight): self.name = name self.


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


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)


		




改版代码:


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()


		





相关文章
|
Java 程序员 C++
Python 面向对象详解!
本文详细介绍了Python中的面向对象编程(OOP),包括类、对象、继承、封装、多态和抽象等核心概念。通过具体示例,解释了如何使用类定义对象的属性和方法,以及如何通过继承实现代码重用。文章还探讨了封装和多态的重要性,并介绍了私有属性和抽象类的使用方法。最后,总结了OOP的四大支柱:封装、抽象、继承和多态,强调了这些概念在Python编程中的应用。适合Java程序员扩展Python编程知识。
626 2
|
Python
你真的会面向对象吗!解密Python“魔术方法”
你真的会面向对象吗!解密Python“魔术方法”
282 0
Python 高级编程与实战:深入理解面向对象与并发编程
本文深入探讨Python的高级特性,涵盖面向对象编程(继承、多态、特殊方法、类与实例属性)、异常处理(try-except、finally)和并发编程(多线程、多进程、异步编程)。通过实战项目如聊天服务器和异步文件下载器,帮助读者掌握这些技术,编写更复杂高效的Python程序。
|
Python
Python面向对象(2)
【10月更文挑战第14天】
334 6
Python面向对象(2)
|
设计模式 程序员 C语言
Python面向对象
【10月更文挑战第13天】
262 2
Python面向对象
|
关系型数据库 开发者 Python
Python编程中的面向对象设计原则####
在本文中,我们将探讨Python编程中的面向对象设计原则。面向对象编程(OOP)是一种通过使用“对象”和“类”的概念来组织代码的方法。我们将介绍SOLID原则,包括单一职责原则、开放/封闭原则、里氏替换原则、接口隔离原则和依赖倒置原则。这些原则有助于提高代码的可读性、可维护性和可扩展性。 ####
|
前端开发 Python
Python编程的面向对象有哪些(二)
Python编程的面向对象(二)—类的多态
242 8
|
IDE Java 开发工具
Python类与面向对象
Python类与面向对象
|
安全 算法 Go
Python面向对象的三大特性
python面向对象编程(OOP)的三大特性是封装、继承和多态。这些特性共同构成了OOP的基础,使得软件设计更加灵活、可维护和可扩展。
400 3
|
数据采集 Java C语言
Python面向对象的高级动态可解释型脚本语言简介
Python是一种面向对象的高级动态可解释型脚本语言。
423 3

推荐镜像

更多