提交 289f2d80 编写于 作者: 檀越@新空间's avatar 檀越@新空间 🐭

fix:面向对象

上级 96dc73b7
...@@ -28,10 +28,3 @@ class Student: ...@@ -28,10 +28,3 @@ class Student:
stu1 = Student("周杰轮", 31) stu1 = Student("周杰轮", 31)
stu2 = Student("林俊节", 36) stu2 = Student("林俊节", 36)
print(stu1 == stu2) print(stu1 == stu2)
#
...@@ -2,19 +2,18 @@ ...@@ -2,19 +2,18 @@
演示面向对象:继承的基础语法 演示面向对象:继承的基础语法
""" """
# 演示单继承 # 演示单继承
class Phone: class Phone:
IMEI = None # 序列号 IMEI = None # 序列号
producer = "ITCAST" # 厂商 producer = "ITCAST" # 厂商
def call_by_4g(self): def call_by_4g(self):
print("4g通话") print("4g通话")
class Phone2022(Phone): class Phone2022(Phone):
face_id = "10001" # 面部识别ID face_id = "10001" # 面部识别ID
def call_by_5g(self): def call_by_5g(self):
print("2022年新功能:5g通话") print("2022年新功能:5g通话")
...@@ -24,6 +23,8 @@ phone = Phone2022() ...@@ -24,6 +23,8 @@ phone = Phone2022()
print(phone.producer) print(phone.producer)
phone.call_by_4g() phone.call_by_4g()
phone.call_by_5g() phone.call_by_5g()
# 演示多继承 # 演示多继承
class NFCReader: class NFCReader:
nfc_type = "第五代" nfc_type = "第五代"
...@@ -52,10 +53,4 @@ phone.call_by_4g() ...@@ -52,10 +53,4 @@ phone.call_by_4g()
phone.read_card() phone.read_card()
phone.write_card() phone.write_card()
phone.control() phone.control()
print(phone.producer) print(phone.producer)
# 演示多继承下,父类成员名一致的场景
...@@ -5,8 +5,8 @@ ...@@ -5,8 +5,8 @@
class Phone: class Phone:
IMEI = None # 序列号 IMEI = None # 序列号
producer = "ITCAST" # 厂商 producer = "ITCAST" # 厂商
def call_by_5g(self): def call_by_5g(self):
print("使用5g网络进行通话") print("使用5g网络进行通话")
...@@ -14,7 +14,7 @@ class Phone: ...@@ -14,7 +14,7 @@ class Phone:
# 定义子类,复写父类成员 # 定义子类,复写父类成员
class MyPhone(Phone): class MyPhone(Phone):
producer = "ITHEIMA" # 复写父类的成员属性 producer = "ITHEIMA" # 复写父类的成员属性
def call_by_5g(self): def call_by_5g(self):
print("开启CPU单核模式,确保通话的时候省电") print("开启CPU单核模式,确保通话的时候省电")
...@@ -26,9 +26,9 @@ class MyPhone(Phone): ...@@ -26,9 +26,9 @@ class MyPhone(Phone):
super().call_by_5g() super().call_by_5g()
print("关闭CPU单核模式,确保性能") print("关闭CPU单核模式,确保性能")
phone = MyPhone() phone = MyPhone()
phone.call_by_5g() phone.call_by_5g()
print(phone.producer) print(phone.producer)
# 在子类中,调用父类成员 # 在子类中,调用父类成员
...@@ -75,6 +75,5 @@ def make_cool(ac: AC): ...@@ -75,6 +75,5 @@ def make_cool(ac: AC):
midea_ac = Midea_AC() midea_ac = Midea_AC()
gree_ac = GREE_AC() gree_ac = GREE_AC()
make_cool(midea_ac) make_cool(midea_ac)
make_cool(gree_ac) make_cool(gree_ac)
...@@ -4,13 +4,11 @@ ...@@ -4,13 +4,11 @@
class Record: class Record:
def __init__(self, date, order_id, money, province): def __init__(self, date, order_id, money, province):
self.date = date # 订单日期 self.date = date # 订单日期
self.order_id = order_id # 订单ID self.order_id = order_id # 订单ID
self.money = money # 订单金额 self.money = money # 订单金额
self.province = province # 销售省份 self.province = province # 销售省份
def __str__(self): def __str__(self):
return f"{self.date}, {self.order_id}, {self.money}, {self.province}" return f"{self.date}, {self.order_id}, {self.money}, {self.province}"
...@@ -17,7 +17,7 @@ class FileReader: ...@@ -17,7 +17,7 @@ class FileReader:
class TextFileReader(FileReader): class TextFileReader(FileReader):
def __init__(self, path): def __init__(self, path):
self.path = path # 定义成员变量记录文件的路径 self.path = path # 定义成员变量记录文件的路径
# 复写(实现抽象方法)父类的方法 # 复写(实现抽象方法)父类的方法
def read_data(self) -> list[Record]: def read_data(self) -> list[Record]:
...@@ -25,7 +25,7 @@ class TextFileReader(FileReader): ...@@ -25,7 +25,7 @@ class TextFileReader(FileReader):
record_list: list[Record] = [] record_list: list[Record] = []
for line in f.readlines(): for line in f.readlines():
line = line.strip() # 消除读取到的每一行数据中的\n line = line.strip() # 消除读取到的每一行数据中的\n
data_list = line.split(",") data_list = line.split(",")
record = Record(data_list[0], data_list[1], int(data_list[2]), data_list[3]) record = Record(data_list[0], data_list[1], int(data_list[2]), data_list[3])
record_list.append(record) record_list.append(record)
...@@ -37,8 +37,7 @@ class TextFileReader(FileReader): ...@@ -37,8 +37,7 @@ class TextFileReader(FileReader):
class JsonFileReader(FileReader): class JsonFileReader(FileReader):
def __init__(self, path): def __init__(self, path):
self.path = path # 定义成员变量记录文件的路径 self.path = path # 定义成员变量记录文件的路径
def read_data(self) -> list[Record]: def read_data(self) -> list[Record]:
f = open(self.path, "r", encoding="UTF-8") f = open(self.path, "r", encoding="UTF-8")
...@@ -54,8 +53,10 @@ class JsonFileReader(FileReader): ...@@ -54,8 +53,10 @@ class JsonFileReader(FileReader):
if __name__ == '__main__': if __name__ == '__main__':
text_file_reader = TextFileReader("D:/2011年1月销售数据.txt") text_file_reader = TextFileReader(
json_file_reader = JsonFileReader("D:/2011年2月销售数据JSON.txt") "/Users/qinyingjie/Documents/idea-workspace/study/python-demo/data/对象/2011年1月销售数据.txt")
json_file_reader = JsonFileReader(
"/Users/qinyingjie/Documents/idea-workspace/study/python-demo/data/对象/2011年2月销售数据JSON.txt")
list1 = text_file_reader.read_data() list1 = text_file_reader.read_data()
list2 = json_file_reader.read_data() list2 = json_file_reader.read_data()
...@@ -63,4 +64,4 @@ if __name__ == '__main__': ...@@ -63,4 +64,4 @@ if __name__ == '__main__':
print(l) print(l)
for l in list2: for l in list2:
print(l) print(l)
\ No newline at end of file
...@@ -13,8 +13,10 @@ from pyecharts.charts import Bar ...@@ -13,8 +13,10 @@ from pyecharts.charts import Bar
from pyecharts.options import * from pyecharts.options import *
from pyecharts.globals import ThemeType from pyecharts.globals import ThemeType
text_file_reader = TextFileReader("D:/2011年1月销售数据.txt") text_file_reader = TextFileReader(
json_file_reader = JsonFileReader("D:/2011年2月销售数据JSON.txt") "/Users/qinyingjie/Documents/idea-workspace/study/python-demo/data/对象/2011年1月销售数据.txt")
json_file_reader = JsonFileReader(
"/Users/qinyingjie/Documents/idea-workspace/study/python-demo/data/对象/2011年2月销售数据JSON.txt")
jan_data: list[Record] = text_file_reader.read_data() jan_data: list[Record] = text_file_reader.read_data()
feb_data: list[Record] = json_file_reader.read_data() feb_data: list[Record] = json_file_reader.read_data()
...@@ -34,11 +36,10 @@ for record in all_data: ...@@ -34,11 +36,10 @@ for record in all_data:
# 可视化图表开发 # 可视化图表开发
bar = Bar(init_opts=InitOpts(theme=ThemeType.LIGHT)) bar = Bar(init_opts=InitOpts(theme=ThemeType.LIGHT))
bar.add_xaxis(list(data_dict.keys())) # 添加x轴的数据 bar.add_xaxis(list(data_dict.keys())) # 添加x轴的数据
bar.add_yaxis("销售额", list(data_dict.values()), label_opts=LabelOpts(is_show=False)) # 添加了y轴数据 bar.add_yaxis("销售额", list(data_dict.values()), label_opts=LabelOpts(is_show=False)) # 添加了y轴数据
bar.set_global_opts( bar.set_global_opts(
title_opts=TitleOpts(title="每日销售额") title_opts=TitleOpts(title="每日销售额")
) )
bar.render("每日销售额柱状图.html") bar.render("每日销售额柱状图.html")
"""
面向对象,数据分析案例,主业务逻辑代码
实现步骤:
1. 设计一个类,可以完成数据的封装
2. 设计一个抽象类,定义文件读取的相关功能,并使用子类实现具体功能
3. 读取文件,生产数据对象
4. 进行数据需求的逻辑计算(计算每一天的销售额)
5. 通过PyEcharts进行图形绘制
"""
此差异已折叠。
此差异已折叠。
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册