python面向对象小练习
时间:2022-06-17
本文章向大家介绍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()
- R语言读CSV、txt文件方式以及read.table read.csv 和readr(大数据读取包)
- JSP与EL表达式重点学习笔记(2)
- Node.js真的无所不能?那些不适用的应用领域分析
- #!/bin/bash 与#!/bin/sh
- 客户端无法连接数据库的小问题(r8笔记第53天)
- Golang事务模型
- 厚土Go学习笔记 | 35. web服务器实现动态路径
- 数据库连接池、dbutil_知识点全掌握
- Golang 序列化之 ProtoBuf
- Golang RPC 之 gRPC
- 解决连通性问题的四种算法
- 使用shell批量生成数据整合式迁移的脚本(r8笔记第52天)
- Jdbc知识点全整理,你值得拥有 (1)
- SSD: Single Shot MultiBox Detector 深度学习笔记之SSD物体检测模型
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- 【C语言简单说】十九:二维数组循环嵌套(1)
- 【C语言简单说】十九:二维数组循环嵌套(2)
- 【C语言简单说】二十:指针基础
- 【C语言简单说】二十一:双重指针基础 (完结)
- 有关 php __autoload 自动加载类函数的用法
- sql 子查询(mysql)
- php 使用PDO,防止sql注入 简单说明
- js (javascript) 中获取年月日信息
- js(javascript)取得当前时间小时,分钟,秒 以及毫秒
- js(javascript) onclick与ondblclick 单击与双击事件
- unity5.x C# 获取屏幕宽度 设置不受重力影响
- unity5.x Translate平移移动 以及GetComponent获取组件
- php 计时器microtime 以及去掉数组重复值array_unique
- php 字符串打散为数组,用逗号分隔出数组
- php in_array 判断数组中是否存在此元素