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

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

python面向对象小练习的更多相关文章

  1. python面向对象小tips

    (一).python鸭子类型 python作为动态语言继承和多态与静态语言(像java)有很大的不同:比如说在java中的某个方法,如果传入的参数是Animal类型,那么传入的对象必须是Animal类 ...

  2. python 面向对象初级篇

    Python 面向对象(初级篇) 概述 面向过程:根据业务逻辑从上到下写垒代码 函数式:将某功能代码封装到函数中,日后便无需重复编写,仅调用函数即可 面向对象:对函数进行分类和封装,让开发" ...

  3. Python 面向对象 基础

    编程范式概述:面向过程 和 面向对象 以及函数式编程 面向过程:(Procedure Oriented)是一种以事件为中心的编程思想. 就是分析出解决问题所需要的步骤,然后用函数把这些步骤一步一步实现 ...

  4. 12岁的少年教你用Python做小游戏

    首页 资讯 文章 频道 资源 小组 相亲 登录 注册       首页 最新文章 经典回顾 开发 设计 IT技术 职场 业界 极客 创业 访谈 在国外 - 导航条 - 首页 最新文章 经典回顾 开发 ...

  5. Python开发【第七篇】:面向对象 和 python面向对象(初级篇)(上)

    Python 面向对象(初级篇)   51CTO同步发布地址:http://3060674.blog.51cto.com/3050674/1689163 概述 面向过程:根据业务逻辑从上到下写垒代码 ...

  6. python 面向对象进阶之内置方法

    一 isinstance(obj,cls)和issubclass(sub,super) 1.1,isinstance(obj,cls)检查是否obj是否是类 cls 的对象 class Foo(obj ...

  7. python面向对象编程进阶

    python面向对象编程进阶 一.isinstance(obj,cls)和issubclass(sub,super) isinstance(obj,cls)检查是否obj是否是类 cls 的对象 1 ...

  8. Python学习笔记【第十一篇】:Python面向对象高级

    isinstance(obj,cls)和issubclass(sub,super) class Person(object): def __init__(self, name, age, sex, n ...

  9. python面向对象入门(1):从代码复用开始

    本文从代码复用的角度一步一步演示如何从python普通代码进化到面向对象,并通过代码去解释一些面向对象的理论.所以,本文前面的内容都是非面向对象的语法实现方式,只有在最结尾才给出了面向对象的简单语法介 ...

随机推荐

  1. 使用Boost program_options控制程序输入

    简介 很多人使用界面来输入数据,本文的程序介绍如何使用Boost的program_options控制输入. 程序中使用了: 1. 短选项 2. 可以指定多个参数的选项 程序 #include < ...

  2. MySQL系列教程(二)

    mySQL执行计划 语法  explain <sql语句> 例如: explain select * from t3 where id=3952602; explain输出解释 +---- ...

  3. npm killed有可能是内存不够, 为Ubuntu增加swap

    参考 http://www.cnblogs.com/owenyang/p/4282283.html 查看swap使用策略 cat /proc/sys/vm/swappiness 0代表尽量使用物理内存 ...

  4. JBOSS EAP 6 系列六 公共模块的jar配置到jboss的modules详细配置

    公司项目中遇到并要解决的问题 1:原则上除了自己写的代码之外,公共的jar不应该都在打包的时候打包到ear里面,这样的话包太大,也不符合的分层的逻辑,在jboss容器内部,每个ear的包重复jar都会 ...

  5. TextView的升级版———AutoCompleteTextView

    TextView的升级版---AutoCompleteTextView AutoCompleteTextView顾名知义,可以自动提示的TextView,还可以提示错误信息. 这里介绍基本的使用,能够 ...

  6. RxJava(二) map操作符用法详解

    欢迎转载,转载请标明出处: http://blog.csdn.net/johnny901114/article/details/51531348 本文出自:[余志强的博客] 1 map操作符的作用 R ...

  7. Cocoa中层(layer)坐标系的极简理解

    大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请多提意见,如果觉得不错请多多支持点赞.谢谢! hopy ;) Cocoa层的坐标系一直理解的不清晰,现在把它整理总结一下: ...

  8. nginx中configure脚本支持的常用选项,拍摄自《Nginx高性能Web服务器详解》

  9. ExpandableListView仿QQ好友列表

    本例中,对ExpandableListView中的数据进行了封装,分为两个JavaBean,一个为Group类表示组信息,一个Child类表示该组下子列表信息: Group: public class ...

  10. 剑指Offer——网易校招内推笔试题+模拟题知识点总结

    剑指Offer--网易校招内推笔试题+模拟题知识点总结 前言 2016.8.2 19:00网易校招内推笔试开始进行.前天晚上利用大约1小时时间完成了测评(这个必须做,关切到你能否参与面试).上午利用2 ...