首页 > 分享 > 【类、抽象与继承(练习)~python】

【类、抽象与继承(练习)~python】

python 练习目录 类的练习简单介绍-1学生的多重身份中西兼备的厨师 继承 练习运行效果如下: 简单介绍-2 抽象 练习运行效果如下: pandas 每日一练:程序运行结果为: 31、计算 popularity 列的中位数程序运行结果为: 32、绘制成绩水平频率分布直方图程序运行效果为: 33、绘制成绩水平密度曲线程序运行效果为: 34、删除最后一列time程序运行结果为:程序运行结果为: 35、将df的第一列与第三列合并为新的一列程序运行结果为: 36、将project列与popularity列合并为新的一列程序运行结果为: 37、计算popularity最大值与最小值之差程序运行结果为: 38、将第一行与最后一行拼接程序运行结果为: 39、将第2行数据添加至末尾程序运行结果为: 40、查看每列的数据类型程序运行结果为:每日一言:持续更新中...

个人昵称:lxw-pro
个人主页:欢迎关注 我的主页
个人感悟: “失败乃成功之母”,这是不变的道理,在失败中总结,在失败中成长,才能成为IT界的一代宗师。

Python 类中包含一个特殊的变量self,它表示当前对象自身,可以访问类的成员
init()方法的作用是初始化对象
静态方法的装饰器为static method
在类中,实例方法可以访问类属性
在类中,类方法可以访问类成员
对象描述的是现实的个体,它是类的实例

类的练习

# 简单类创建练习 class Maleguests: sex = "M" marriage = "False" def hello(self): print("各位大佬好") zhangsan = Maleguests() print(zhangsan.sex) print(zhangsan.marriage) zhangsan.hello() 1234567891011121314'

简单介绍-1

# 简单介绍 class Maleguests: def __init__(self, name, age, city): print("各位大佬好") self.name = name self.age = age self.city = city def js(self): print(f"我叫{self.name}") print(f"我今年{self.age}岁") print(f"我来自{self.city}") lxw = Maleguests('Lxw', 21, '贵州') lxw.js() pro = Maleguests('Pro', 1, '贵州') pro.js()

1234567891011121314151617181920'

学生的多重身份

# 学生的多重身份 class Stu(): stu_no = '001' def study(self): print("使用python编程") class Chi(): sta = "孩子" def care(self): print("照顾父母") class Son(Stu, Chi): pass lxw = Son() print(lxw.stu_no) print(lxw.sta) lxw.study() lxw.care()

12345678910111213141516171819202122232425'

中西兼备的厨师

# 中西兼备的厨师 class master(): sta = "厨师" def cake(self): print("制作手抓饼") def cook(self): print("制作传统煎饼果子") class app(master): def cook(self): print("制作中西方口味融合的煎饼果子") print("师傅") shifu = master() print(shifu.sta) shifu.cake() shifu.cook() print("徒弟") tudi = app() print(tudi.sta) tudi.cake() tudi.cook()

123456789101112131415161718192021222324252627282930'

————————————————————————————————————————————

继承 练习

问题描述
创建父类Pet,在父类中,使用构造方法定义name、age、varietie,color四个实例属性,定义两个方法eat和bark,重写__str__()方法,使得打印对象时将对象的属性全部打印输出。

要求
创建子类Dog类和子类Cat类,在两个子类中要求对方法eat和方法bark进行重写,在子类Cat中,重写构造方法,增加实例属性sex,重写__str__()方法,要求打印输出时在父类的基础上增加sex属性。创建宠物店类PetShop,创建实例属性shopName和petList,属性petList的值为宠物列表,定义方法showPets,要求该方法打印宠物店名称和宠物列表。
通过Dog类、Cat类和PetShop类创建对象,然后对三个类中的方法进行调用,查看运行结果。

class Pet: def __init__(self, name, age, variety, color): self.name = name self.age = age self.variety = variety self.color = color def cat(self): print("宠物要吃东西") def bark(self): print("宠物会叫唤") def __str__(self): return f"名字: {self.name}, 年龄: {self.age}, 品种: {self.variety}, 颜色:{self.color}" class Dog(Pet): def eat(self): print(self.name+"吃骨头") def bark(self): print(self.name+"汪汪叫") def guardHose(self): print(self.name+"看家护院") class Cat(Pet): def __init__(self, name, age, variety, color, sex): super(Cat, self).__init__(name, age, variety, color) self.sex = sex def eat(self): print(self.name+"喜欢吃鱼") def bark(self): print(self.name+"会喵喵叫") def catchMice(self): print(self.name+"会抓老鼠") def __str__(self): str = super(Cat, self).__str__() str += ',性别:{0}'.format(self.sex) return str class PetShop: def __init__(self, store_name, *pet_list): self.store_name = store_name self.pet_list = pet_list def showPets(self): if len(self.pet_list) == 0: print(self.store_name+"暂无宠物") return print(f"{self.store_name}有{len(self.pet_list)}个宠物,它们分别是:") for pet in self.pet_list: print(pet) dog1 = Dog('旺财', 3, '金毛', '黄色') dog1.cat() dog1.bark() dog1.guardHose() print("-------------------------") cat1 = Cat('嘟嘟', 2, '咖啡', '灰色', '男') cat1.eat() cat1.bark() cat1.catchMice() print("---------------------------") dog2 = Dog('黑贝', 3, '牧羊犬', '黑色') cat2 = Cat('糖果', 3, '布偶', '白色', '女') petshop = PetShop('乐派宠物店', dog1, dog2, cat1, cat2) petshop.showPets()

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182' 运行效果如下:

在这里插入图片描述

简单介绍-2

class A: name = "AA" def Aprint(self): print("A类的方法") class B(A): def Aprint(self, X): print("B类的方法:" + X) class C(B): def Aprint(self,name,age): print("My name is ", name, "I am ", age, "years old!") c1 = C() c1.Aprint("lxw_pro", 21) # 调用父类的方法

1234567891011121314151617181920'

————————————————————————————————————————————

抽象 练习

问题描述

设计一个宠物口粮花费计算系统(按月计算),其中包括父类Pet,子类Dog、Cat、Pig,提供宠物信息,计算出一个月内该宠物需要多少口粮。其中每只狗狗一天能够吃掉狗粮1KG,狗粮的价格为4元/KG;每只小猫每天能够吃掉0.25KG,猫粮价格为5元/KG,每只宠物猪每天能够吃掉1.5KG猪粮,猪粮的价格为3元/KG。

import abc class Pet(metaclass=abc.ABCMeta): def __init__(self, name, age): self.name = name self.age = age @abc.abstractmethod def mealPay(self): pass class Dog(Pet): def mealPay(self, name): print(f"我是一只宠物狗,我的名字叫{name}") money = 1*4*30 print(f"我每个月的生活费需要{money}元") class Cat(Pet): def mealPay(self, name): print(f"我是一只宠物猫,我的名字叫{name}") money = 0.25*5*30 print(f"我每个月的生活费需要{money}元") class Pig(Pet): def mealPay(self, name): print(f"我是一只宠物猪猪,我的名字叫{name}") money = 1.5*3*30 print(f"我每个月的生活费需要{money}元") while True: print("宠物月花费计算系统:n1, 狗狗n2, 猫猫n3, 猪猪") choice = input("请输入您的选择:(输入0可退出程序)") if choice == '0': print("已退出程序!") break if choice == '1': d1 = Dog('大壮', 3) d1.mealPay(d1.name) elif choice == '2': c1 = Cat('圆圆', 2) c1.mealPay(c1.name) elif choice == '3': p1 = Pig('嘟嘟', 2) p1.mealPay(p1.name) else: print("您的输入有误,请重新输入:")

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253 运行效果如下:

在这里插入图片描述
————————————————————————————————————————————

pandas 每日一练:

# -*- coding = utf-8 -*- # @Time : 2022/7/25 14:10 # @Author : lxw_pro # @File : pandas-7 练习.py # @Software : PyCharm import pandas as pd import numpy as np import matplotlib.pyplot as plt df = pd.read_excel('text5.xlsx') print(df) 123456789101112 程序运行结果为:

Unnamed: 0 Unnamed: 0.1 project ... test_time date time 0 0 00:00:00 Python ... 2022-06-20 18:30:20 2022-06-20 18:30:20 1 1 1 Java ... 2022-06-18 19:40:20 2022-06-18 19:40:20 2 2 2 C ... 2022-06-08 13:33:20 2022-06-08 13:33:20 3 3 3 MySQL ... 2021-12-23 11:26:20 2021-12-23 11:26:20 4 4 4 Linux ... 2021-12-20 18:20:20 2021-12-20 18:20:20 5 5 5 Math ... 2022-07-20 16:30:20 2022-07-20 16:30:20 6 6 6 English ... 2022-06-23 15:30:20 2022-06-23 15:30:20 7 7 7 Python ... 2022-07-19 09:30:20 2022-07-19 09:30:20 [8 rows x 7 columns] 12345678910

31、计算 popularity 列的中位数

zws = np.median(df['popularity']) print(f"popularity列的中位数为:{zws}") 12 程序运行结果为:

popularity列的中位数为:143.0 1

32、绘制成绩水平频率分布直方图

df.popularity.plot(kind='hist') plt.show() 123 程序运行效果为:

在这里插入图片描述

33、绘制成绩水平密度曲线

df.popularity.plot(kind='kde', xlim=(0, 150)) plt.show() 123 程序运行效果为:

在这里插入图片描述

34、删除最后一列time

法一:

del df['time'] print("删除最后一列time后的表为:n", df) 12 程序运行结果为:

删除最后一列time后的表为: Unnamed: 0 Unnamed: 0.1 project popularity test_time date 0 0 00:00:00 Python 95 2022-06-20 18:30:20 2022-06-20 1 1 1 Java 92 2022-06-18 19:40:20 2022-06-18 2 2 2 C 145 2022-06-08 13:33:20 2022-06-08 3 3 3 MySQL 141 2021-12-23 11:26:20 2021-12-23 4 4 4 Linux 84 2021-12-20 18:20:20 2021-12-20 5 5 5 Math 148 2022-07-20 16:30:20 2022-07-20 6 6 6 English 146 2022-06-23 15:30:20 2022-06-23 7 7 7 Python 149 2022-07-19 09:30:20 2022-07-19 12345678910

法二:

df.drop(columns=['date'], inplace=True) print("删除列date后的表为:n", df) 12 程序运行结果为:

删除列date后的表为: Unnamed: 0 Unnamed: 0.1 project popularity test_time 0 0 00:00:00 Python 95 2022-06-20 18:30:20 1 1 1 Java 92 2022-06-18 19:40:20 2 2 2 C 145 2022-06-08 13:33:20 3 3 3 MySQL 141 2021-12-23 11:26:20 4 4 4 Linux 84 2021-12-20 18:20:20 5 5 5 Math 148 2022-07-20 16:30:20 6 6 6 English 146 2022-06-23 15:30:20 7 7 7 Python 149 2022-07-19 09:30:20 12345678910

35、将df的第一列与第三列合并为新的一列

df['test'] = df['project']+['test-time'] print("将df第一列与第三列合并为新的一列后的表为:n", df) 12 程序运行结果为:

将df第一列与第三列合并为新的一列后的表为: Unnamed: 0 Unnamed: 0.1 ... test_time test 0 0 00:00:00 ... 2022-06-20 18:30:20 Pythontest-time 1 1 1 ... 2022-06-18 19:40:20 Javatest-time 2 2 2 ... 2022-06-08 13:33:20 Ctest-time 3 3 3 ... 2021-12-23 11:26:20 MySQLtest-time 4 4 4 ... 2021-12-20 18:20:20 Linuxtest-time 5 5 5 ... 2022-07-20 16:30:20 Mathtest-time 6 6 6 ... 2022-06-23 15:30:20 Englishtest-time 7 7 7 ... 2022-07-19 09:30:20 Pythontest-time [8 rows x 6 columns] 1234567891011

36、将project列与popularity列合并为新的一列

df['text'] = df['project']+df['popularity'].map(str) print("将project列于popularity列合并为新的一列后的表为:n", df) 12 程序运行结果为:

将project列于popularity列合并为新的一列后的表为: Unnamed: 0 Unnamed: 0.1 ... test text 0 0 00:00:00 ... Pythontest-time Python95 1 1 1 ... Javatest-time Java92 2 2 2 ... Ctest-time C145 3 3 3 ... MySQLtest-time MySQL141 4 4 4 ... Linuxtest-time Linux84 5 5 5 ... Mathtest-time Math148 6 6 6 ... Englishtest-time English146 7 7 7 ... Pythontest-time Python149 [8 rows x 7 columns] 1234567891011

37、计算popularity最大值与最小值之差

print(df[['popularity']].apply(lambda x: x.max() - x.min())) 1 程序运行结果为:

popularity 65 dtype: int64 12

38、将第一行与最后一行拼接

print(pd.concat([df[:1], df[-2:-1]])) 1 程序运行结果为:

Unnamed: 0 Unnamed: 0.1 ... test text 0 0 00:00:00 ... Pythontest-time Python95 6 6 6 ... Englishtest-time English146 [2 rows x 7 columns] 1234

39、将第2行数据添加至末尾

df = df.append(df.loc[2]) print("将第2行数据添加至末尾的表为:n", df) 12 程序运行结果为:

将第2行数据添加至末尾的表为: Unnamed: 0 Unnamed: 0.1 ... test text 0 0 00:00:00 ... Pythontest-time Python95 1 1 1 ... Javatest-time Java92 2 2 2 ... Ctest-time C145 3 3 3 ... MySQLtest-time MySQL141 4 4 4 ... Linuxtest-time Linux84 5 5 5 ... Mathtest-time Math148 6 6 6 ... Englishtest-time English146 7 7 7 ... Pythontest-time Python149 2 2 2 ... Ctest-time C145 [9 rows x 7 columns] 123456789101112

40、查看每列的数据类型

lsj = df.dtypes print("查看每列的数据类型为:n", lsj) 12 程序运行结果为:

查看每列的数据类型为: Unnamed: 0 int64 Unnamed: 0.1 object project object popularity int64 test_time datetime64[ns] test object text object dtype: object 123456789 每日一言:

人生不能后悔,只能遗憾,因为遗憾只是在感叹错过,后悔却是否定了自己曾经的选择!!

持续更新中…

点赞,你的认可是我创作的动力!
收藏,你的青睐是我努力的方向!
评论,你的意见是我进步的财富!
关注,你的喜欢是我长久的坚持!
在这里插入图片描述

欢迎关注微信公众号【程序人生6】,一起探讨学习哦!!!

相关知识

java特性封装、抽象、继承、多态理论
Java猫和狗(继承,多态,抽象,接口版)下
python 多重继承
Python面向对象高级编程——多重继承
python定义一个dog类 类属性有名字
Java猫和狗(继承,多态,抽象,接口版)上
[Java] 继承与抽象类相关题目
案例驱动式Python学习笔记【第六篇】电子宠物
Python———面向对象编程
Educoder–Java面向对象 继承和多态综合练习 第1关:练习

网址: 【类、抽象与继承(练习)~python】 https://m.mcbbbk.com/newsview578607.html

所属分类:萌宠日常
上一篇: 《宠物小精灵[电影解说]》电影高
下一篇: 宠物猪如何玩喂养