一,对象基础知识
1使用对象组织数据
二,类的成员方法
1类的定义与使用class
创建方法的语法:def 方法名(self,形参1………..形参N):(虽然self在参数列表中,但是传参的时候可以忽略)print(f"hello,my name is{self .name}")print(f"hello,my name is {self .name},{msg}")stu_1.say_hi2("look my eyes")三,内置方法(魔术方法)
1.构造方法__init__()
2构造类对象时,将传入参数自动传递给__init__方法使用def__init__(self,name,age,tel):stu=student("张三",18,"123456")可以省略定义name=None的环节,在self.name会自动定义def__init__(self,name,age,adr):age=int(input("请输入学生年龄:"))stu=student(name,age,adr)print(f"学生{i}的姓名是{stu.name},年龄是{stu.age},地址是{stu.adr}")2字符串方法__str__
def__init__(self, name, age):stu=student("xiaoming", 18)def__init__(self, name, age):returnf"student类对象,name:{self.name}, age:{self.age}"stu=student("xiaoming", 18)3.小于符号的比较__it __
def__init__(self, name, age):returnself.age < other.agestu1=student('xiaoming', 18)stu2=student('xiaohong', 17)print(stu14.小于等于比较符号方法__le__
def__init__(self, name, age):returnself.age <= other.agestu1=student('xiaoming', 17)stu2=student('xiaohong', 17)5.比较运算符实现__eq__
如果不添加__eq__。使用==符号时,默认比较的是内存地址,结果必然是falsedef__init__(self, name, age):returnself.age == other.agestu1=student('xiaoming', 17)stu2=student('xiaohong', 18)总结
四,封装
私有成员
def__keep_single_core(self):phone.__keep_single_core()def__keep_single_core(self):私有成员无法被类对象使用,但是可以被类中的其它成员使用def__keep_single_core(self):self.__keep_single_core()五,继承
(1)继承的基础语法
1单继承
2多继承
classmyphone(phone,NFCReader,remotocontrol):多继承中,如果有同名父类属性,先继承的优先级高于后继承3 pass语句
如类中没有想要书写的语句,但是不书写会有语法报错,使用pass可以进行替代,表示空。(2)复写
(3)调用父类同名成员
方式二:使用super()调用父类成员
print(f"父类的属性为{super().producer}")六,多态
指的是多种状态:完成某个行为时,使用不同的对象会得到的不同状态同样的行为(函数),传入不同的对象,得到不同的状态1抽象类(接口)
包含抽象方法的类,没有具体的实现方法(pass)跳过也是对子类的软性约束,要求子类必须复写(实现)父类的一些方法来配合使用print("Midea_AC: TestHot")print("Geili_AC: TestHot")七,类型注解
在代码中设计数据交互的地方,提供数据类型注解(显示说明)1帮助第三方IDE工具(如pycharm)对代码进行类型推断,协助做代码提示1变量的类型注解
但以上都为较为直观,在实际写程序中无需编写,但是以下情况是重要的运用场景var_1:int=random.randint(0, 100)var_2:dict=json.loads(data)注意:类型注解仅仅是提示性的,不是决定性的。下面明显的错误也不会报错
2.函数(方法)类型注解
2.1形参类型注解
2.2返回值类型注解
deffunc(data:list)->list:注意:类型注解仅仅是提示性的,不是决定性的。
3Union联合类型注解
需要先导包 from typing import Unionmy_list:list[Union[int,str]]=[1,2,3,"a","b","c"]deffunc(data:Union[int,str])->Union[int,str]:八,综合案例:数据处理
def__init__(self,data,order_id,money,province):日期
订单id
金额
self.province=province#省份returnf"{self.date},{self.order_id},{self.money},{self.province}"2设计一个抽象类,定义文件读取的相关功能,并使用子类实现具体运用from date_define import Recorddefread_date(self)->List[Record]:classTextFileReader(FileReader):defread_date(self) ->List[Record]:f=open(self.path,"r",encoding="utf-8")for line in f.readlines():data_list=line.split(",")record=Record(data_list[0],data_list[1],int(data_list[2]),data_list[3])record_list.append(record)classJsonFileReader(FileReader):defread_date(self) ->List[Record]:f=open(self.path,"r",encoding="utf-8")for line in f.readlines():data_dict=json.loads(line)record=Record(data_dict["date"],data_dict["order_id"],int(data_dict["money"]),data_dict["province"])record_list.append(record)if __name__ == "__main__":text_file_reader=TextFileReader("D:/2011年1月销售数据.txt")json_file_reader=JsonFileReader("D:/2011年2月销售数据JSON.txt")list1=text_file_reader.read_date()list2=json_file_reader.read_date()from file_define import FileReader,TextFileReader,JsonFileReaderfrom data_define import Recordtext_file_reader=TextFileReader("D:/2011年1月销售数据.txt")json_file_reader=JsonFileReader("D:/2011年2月销售数据JSON.txt")jan_data:list[ Record]=text_file_reader.read_date()feb_data:list[ Record]=json_file_reader.read_date()all_date:list[ Record]=jan_data+feb_dataif record.date in data_dict:data_dict[record.date]+=record.moneydata_dict[record.date]=record.moneybar=Bar(init_opts=InitOpts(theme=ThemeType.LIGHT))bar.add_xaxis(list(data_dict.keys()))bar.add_yaxis("销售额",list(data_dict.values()),label_opts=LabelOpts(is_show=False))bar.set_global_opts(title_opts=TitleOpts(title="每日销售数据"))bar.render("每日销售数据.html")