python强化阶段
这里写目录标题
- 一、面向对象
- 1、初识对象
- 2、类成员方法
- 3、类和对象
- 4、构造方法
- 5、其他内置方法
- (1)__str__字符串方法
- (2)__lt__小于符号比较方法
- (3)__le__小于等于符号比较方法
- (4)__eq__,判断是否相等实现方法
- 6、封装
- 7、继承
- 单继承
- 多继承
- 复写和使用父类成员
- 8、类型注解
- 1、变量类型注解
- 2、函数(方法)的类型注解
- 3、Union类型
一、面向对象
1、初识对象
# 1、设计一个类(设计一张登记表)classStudent:name=None# 姓名gender=None# 性别nationality=None# 国籍native_place=None# 籍贯age=None# 年龄# 2、创建一个对象(创建一个登记表实例)stu_1=Student()# 3、对象属性赋值(填表)stu_1.name="张三"stu_1.gender="男"stu_1.nationality="中国"stu_1.native_place="北京"stu_1.age=20# 4、获取对象中记录的信息(查询表)print(stu_1.name)print(stu_1.gender)print(stu_1.nationality)print(stu_1.native_place)print(stu_1.age)2、类成员方法
classStudent:name=None# 姓名age=None# 年龄defsay_hi(self):print(f"Hi, my name is{self.name}")defsay_hi2(self,msg):print(f"Hi, my name is{self.name},{msg}")stu=Student()stu.name="Tom"stu.say_hi()stu.say_hi2("I am a student")3、类和对象
classClock:id=Noneprice=Nonedefring(self):importwinsound winsound.Beep(2000,3000)clock1=Clock()clock1.id="001"clock1.prince=19.99print(f"闹钟ID:{clock1.id},价格:{clock1.price}")clock1.ring()clock2=Clock()clock2.id="002"clock2.prince=19.99print(f"闹钟ID:{clock2.id},价格:{clock2.price}")clock2.ring()4、构造方法
引出构造方法
两个下划线
classStudent():name=Noneage=Nonetel=None# 可省略def__init__(self,name,age,tel):self.name=name self.age=age self.tel=tel stu1=Student("Tom",19,22)stu2=Student("Jack",18,29)print(stu1.name,stu1.age,stu1.tel)print(stu2.name,stu2.age,stu2.tel)5、其他内置方法
(1)__str__字符串方法
通过__str__字符串方法控制类转换为字符串的行为
classStudent():def__init__(self,name,age):self.name=name self.age=agedef__str__(self):returnf"Student类对象,name:{self.name},age:{self.age}"stu=Student("张三",20)print(stu)print(str(stu))(2)__lt__小于符号比较方法
classStudent():def__init__(self,name,age):self.name=name self.age=agedef__lt__(self,other):returnself.age<other.age stu1=Student("李四",22)stu2=Student("王五",21)print(stu1<stu2)print(stu1>stu2)(3)__le__小于等于符号比较方法
(4)eq,判断是否相等实现方法
6、封装
面向对象三个特性:
封装、继承、多态
# 定义一个类,内含私有成员方法和私有成员方法classPhone:__current_voltage=Nonedef__keep_single_core(self):print("让CPU单核运行")phone=Phone()phone.__keep_single_core()# 报错,私有方法不能被外部调用print(phone.__current_voltage)# 报错,私有成员不能被外部访问classPhone:__current_voltage=1def__keep_single_core(self):print("让CPU单核运行")defcal_by_5g(self):ifself.__current_voltage>=1:print("5G通话已开启")else:print("5G通话未开启,电压不足")phone=Phone()phone.cal_by_5g()# 输出:5G通话未开启,电压不足phone.__current_voltage=1总结
练习
# 练习classPhone:__is_5g_enable=Falsedef__check_5g(self):ifself.__is_5g_enable:print("5g已开启")else:print("5g未开启,使用4g网络")defcall_by_5g(self):self.__check_5g()print("正在通话")phone=Phone()phone.call_by_5g()# 输出:5g未开启7、继承
单继承
#演示单继承classPhone:IMEI=Noneproducer="HM"defcall_by_4g(self):print("4g打电话")classPhone2022(Phone):face_id="10001"defcall_by_5g(self):print("2022年新功能:5g打电话")phone=Phone2022()print(phone.producer)print(phone.face_id)phone.call_by_4g()phone.call_by_5g()多继承
# 多继承classPhone:IMEI=Noneproducer="HM"defcall_by_4g(self):print("4g打电话")classNFCReader:nfc_type="第五代"producer="IT"defread_card(self):print("读卡")defwrite_card(self):print("写卡")classRemoteControl:rc_type="红外遥控"defcontrol(self):print("红外遥控开启")classMyPhone(Phone,NFCReader,RemoteControl):passphone=MyPhone()phone.call_by_4g()phone.read_card()print(phone.producer)//谁先继承先打印谁复写和使用父类成员
1、复写
# 复写使用父类成员classPhone:IMEI=Noneproducer="HM"defcall_by_4g(self):print("4g打电话")classPhone2022(Phone):producer="IT"defcall_by_4g(self):print("2022年新功能:4g打电话")print("关闭CPU单核模式,确保性能")phone=Phone2022()phone.call_by_4g()print(phone.producer)
2、调用父类成员
# 复写使用父类成员classPhone:IMEI=Noneproducer="HM"defcall_by_4g(self):print("4g打电话")classPhone2022(Phone):producer="IT"defcall_by_4g(self):print("2022年新功能:4g打电话")# 方式1:调用父类方法# Phone.call_by_4g(self)# print(f"父类的厂商是{Phone.producer}")# 方式2:调用父类方法super().call_by_4g()print(f"父类的厂商是{super().producer}")print("关闭CPU单核模式,确保性能")phone=Phone2022()phone.call_by_4g()print(phone.producer)8、类型注解
因为Pycharm无法通过代码确定应传入什么类型,我们需要使用类型注解。
1、变量类型注解
# 变量类型注解# 基础数据类型注解importrandomfrompandas._libsimportjson# van_1: int = 10# var_2: str = "hello"# var_3: bool = True# 类对象类型注解classStudent:passstu:Student=Student()# 基础容器类型注解my_list:list=[1,2,3]my_tuple:tuple=(1,2,3)my_dict:dict={"key":"value"}# 容器类型详细注解my_list:list[int]=[1,2,3]my_tuple:tuple[int,str,bool]=(1,"itheima",True)my_dict:dict[str,int]={"key":666}# 在注释中使用类型注解var_1=random.randint(1,10)# type:intvar_2=json.loads('{"name":"itheima","age":18}')# type:dict[str,int]deffunc():return10var_3=func()# type:int类型注解的限制
2、函数(方法)的类型注解
# 对形参进行类型注解defadd(x:int,y:int):returnx+y add(1,2)# 对返回值进行类型注解deffunc(data:list)->list:returndata