• python面向对象小练习



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


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





  • 相关阅读:
    SqlSugar学习总结5映射操作
    从新回归Vue之3.0(一):Vite,创建
    ​从新回归Vue之3.0(七):安装scss并配置全局样式文件,.env.环境变量配置和读取
    从新回归Vue之3.0(四):动态组件,vuex
    从新回归Vue之3.0(二):setup,defineProps,defineEmits,变量,defineExpose
    从新回归Vue之3.0(三)Watch,WatchEffect,生命周期
    ​从新回归Vue之3.0(六):配置vite.config.ts,tsconfig.json
    ​从新回归Vue之3.0(五):router配置,引入elementplus
    Vue 3 render SVG tag bug All In One
    Swift Function argument All In One
  • 原文地址:https://www.cnblogs.com/wangyaning/p/7854004.html
Copyright © 2020-2023  润新知