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

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. 2018 dnc 公司案例大全,迎接.NET Core开源新时代

    2018 dnc 公司案例大全,迎接.NET Core开源新时代   dnc = .NET Core.dotnet Core dnc是微软新一代主力编程平台,开源.免费.跨平台.轻量级.高性能,支持L ...

  2. Docker容器的运用

    Docker 利用容器来运行应用. 容器是从镜像创建的运行实例.它可以被启动.开始.停止.删除.每个容器都是相互隔离的.保证安全的平台. 可以把容器看做是一个简易版的 Linux 环境(包括root用 ...

  3. Java第2次实验提纲(Java基本语法与类库)

    1. 使用Git克隆(clone)项目到你的Eclipse项目中 见以下参考资料中的3 从码云将项目clone到你的电脑 重要提示: 使用Git来管理你的代码以后,当你在本机Eclipse项目中开始编 ...

  4. Freemarker商品详情页静态化服务调用处理

    --------------------------------------------------------------------------------------------- [版权申明: ...

  5. linux网络编程之一-----多播(组播)编程

    什么是多播 组播(Multicast)是网络一种点对多(one to many)的通信方式,通过报文复制完成网络中一台server对应多台接收者的高效数据传 送.对其形象的比喻就是类似于广播电台和电视 ...

  6. Thread Pools

    许多程序会动态创建数十个设置上百个线程.举个例子,一个web服务器可能在每一个新到来的请求时创建一个新线程,然后在请求完成后将其终止. 然而,创建一个新线程将会带来一定的耗费:它需要在内核中创建自身必 ...

  7. 详解EBS接口开发之供应商导入(补充)--错误信息处理

    check reject details on records of AP_SUPPLIER_INT SELECT s.parent_table,s.reject_lookup_code,S.LAST ...

  8. SKSpriteNode对象初始化在iPhone 6 plus中显示不正确的分析及解决

    一个SpriteKit项目在其他设备上运行都无问题(无论是真机或是模拟器),但是在iPhone6 Plus上会出现精灵对象纹理被过度放大的现象: 从上图中大家可以看到无论是主角或是道具球都过大了. 看 ...

  9. 1.物理系统PhysicsWorld,RayCast

     1 3.0物理系统PhysicsWorld T07PhysicsWorld.h #ifndef __T07PhysicsWorld_H__ #define __T07PhysicsWorld_H ...

  10. JAVA进阶之旅(一)——增强for循环,基本数据类型的自动拆箱与装箱,享元设计模式,枚举的概述,枚举的应用,枚举的构造方法,枚举的抽象方法

    JAVA进阶之旅(一)--增强for循环,基本数据类型的自动拆箱与装箱,享元设计模式,枚举的概述,枚举的应用,枚举的构造方法,枚举的抽象方法 学完我们的java之旅,其实收获还是很多的,但是依然还有很 ...