python面向对象小项目,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()

da9abe536e399ef46597c43d393b9b75.png

本文同步分享在 博客“shiter”(CSDN)。

如有侵权,请联系 support@oschina.cn 删除。

本文参与“OSC源创计划”,欢迎正在阅读的你也加入,一起分享。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Python实现一些小道具小功能(Python implements some small props) Image-Edit 几个基本的图片编辑工具,包括一下功能: 文件:打开,保存,退出 编辑:放大,缩小,灰度,亮度,旋转,截图 变换:傅里叶变换,离散余弦变换,Radon变换 噪声:高斯,椒盐,斑点,泊松 滤波:高通,低通,平滑,锐化 直方图统计:R直方图,G直方图,B直方图 图像增强:伪彩色,真彩色,直方图均衡,NTSC颜色模型,YCbCr颜色模型,HSV颜色模型 阈值分割 生态学处理 特征提取 图像分类与识别 Beautify-Camera 主要功能 文件:打开,保存,打开摄像头 操作:还原,人脸识别 滤镜:怀旧,木刻,灰色,彩色,风格化,增强细节 调节:亮度,饱和度,伽马变换,边缘保持 磨皮美白:美白度,磨皮程度,磨皮精度 灰度直方图 Calculator 主要功能 基本的加减乘除和开根号等运算 Painting绘画 主要功能 File:新建画板,打开图片,保存图片 Edit:复制,清空画板 Image:翻转 工具:基本画笔,橡皮擦,图形创建工具等 编辑区,色彩调节区,字体调节区等 NotePad 主要功能 基本文本编辑,类似于记事本 RandomPassWord 主要功能 随机生成一串密码,包括大小写字母,数字,符号,可指定长度 Browser 主要功能 基本浏览器功能 MusicPlayer 主要功能 音乐播放器 PyTunes 主要功能 轻量级音乐播放器
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值