日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python面向对象小练习

發布時間:2025/5/22 python 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python面向对象小练习 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.


就是幾個動物,自動排列生成什么的


class Animal(object):def __init__(self,name,weight):self.name = nameself.weight = weightdef eat(self):self.weight +=1def 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 +=1def 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 +=1def 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 +=1def 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 = zooreturn zooanimal = [] 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 = nameself.weight = weightdef eat(self):self.weight +=1def 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 +=1def 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 +=1def 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 +=1def 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 containerdef 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()





轉載于:https://www.cnblogs.com/wangyaning/p/7854004.html

總結

以上是生活随笔為你收集整理的python面向对象小练习的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。