资讯动态

python强化阶段

发布时间:2026/10/24 2:06:30 来源:尧图企业网站定制
这里写目录标题一、面向对象1、初识对象2、类成员方法3、类和对象4、构造方法5、其他内置方法1__str__字符串方法2__lt__小于符号比较方法3__le__小于等于符号比较方法4__eq__判断是否相等实现方法6、封装7、继承单继承多继承复写和使用父类成员8、类型注解1、变量类型注解2、函数方法的类型注解3、Union类型一、面向对象1、初识对象# 1、设计一个类设计一张登记表classStudent:nameNone# 姓名genderNone# 性别nationalityNone# 国籍native_placeNone# 籍贯ageNone# 年龄# 2、创建一个对象创建一个登记表实例stu_1Student()# 3、对象属性赋值填表stu_1.name张三stu_1.gender男stu_1.nationality中国stu_1.native_place北京stu_1.age20# 4、获取对象中记录的信息查询表print(stu_1.name)print(stu_1.gender)print(stu_1.nationality)print(stu_1.native_place)print(stu_1.age)2、类成员方法classStudent:nameNone# 姓名ageNone# 年龄defsay_hi(self):print(fHi, my name is{self.name})defsay_hi2(self,msg):print(fHi, my name is{self.name},{msg})stuStudent()stu.nameTomstu.say_hi()stu.say_hi2(I am a student)3、类和对象classClock:idNonepriceNonedefring(self):importwinsound winsound.Beep(2000,3000)clock1Clock()clock1.id001clock1.prince19.99print(f闹钟ID:{clock1.id},价格{clock1.price})clock1.ring()clock2Clock()clock2.id002clock2.prince19.99print(f闹钟ID:{clock2.id},价格{clock2.price})clock2.ring()4、构造方法引出构造方法两个下划线classStudent():nameNoneageNonetelNone# 可省略def__init__(self,name,age,tel):self.namename self.ageage self.teltel stu1Student(Tom,19,22)stu2Student(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.namename self.ageagedef__str__(self):returnfStudent类对象name:{self.name},age:{self.age}stuStudent(张三,20)print(stu)print(str(stu))2__lt__小于符号比较方法classStudent():def__init__(self,name,age):self.namename self.ageagedef__lt__(self,other):returnself.ageother.age stu1Student(李四,22)stu2Student(王五,21)print(stu1stu2)print(stu1stu2)3__le__小于等于符号比较方法4eq判断是否相等实现方法6、封装面向对象三个特性封装、继承、多态# 定义一个类内含私有成员方法和私有成员方法classPhone:__current_voltageNonedef__keep_single_core(self):print(让CPU单核运行)phonePhone()phone.__keep_single_core()# 报错私有方法不能被外部调用print(phone.__current_voltage)# 报错私有成员不能被外部访问classPhone:__current_voltage1def__keep_single_core(self):print(让CPU单核运行)defcal_by_5g(self):ifself.__current_voltage1:print(5G通话已开启)else:print(5G通话未开启电压不足)phonePhone()phone.cal_by_5g()# 输出5G通话未开启电压不足phone.__current_voltage1总结练习# 练习classPhone:__is_5g_enableFalsedef__check_5g(self):ifself.__is_5g_enable:print(5g已开启)else:print(5g未开启使用4g网络)defcall_by_5g(self):self.__check_5g()print(正在通话)phonePhone()phone.call_by_5g()# 输出5g未开启7、继承单继承#演示单继承classPhone:IMEINoneproducerHMdefcall_by_4g(self):print(4g打电话)classPhone2022(Phone):face_id10001defcall_by_5g(self):print(2022年新功能5g打电话)phonePhone2022()print(phone.producer)print(phone.face_id)phone.call_by_4g()phone.call_by_5g()多继承# 多继承classPhone:IMEINoneproducerHMdefcall_by_4g(self):print(4g打电话)classNFCReader:nfc_type第五代producerITdefread_card(self):print(读卡)defwrite_card(self):print(写卡)classRemoteControl:rc_type红外遥控defcontrol(self):print(红外遥控开启)classMyPhone(Phone,NFCReader,RemoteControl):passphoneMyPhone()phone.call_by_4g()phone.read_card()print(phone.producer)//谁先继承先打印谁复写和使用父类成员1、复写# 复写使用父类成员classPhone:IMEINoneproducerHMdefcall_by_4g(self):print(4g打电话)classPhone2022(Phone):producerITdefcall_by_4g(self):print(2022年新功能4g打电话)print(关闭CPU单核模式确保性能)phonePhone2022()phone.call_by_4g()print(phone.producer)2、调用父类成员# 复写使用父类成员classPhone:IMEINoneproducerHMdefcall_by_4g(self):print(4g打电话)classPhone2022(Phone):producerITdefcall_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单核模式确保性能)phonePhone2022()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:StudentStudent()# 基础容器类型注解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_1random.randint(1,10)# type:intvar_2json.loads({name:itheima,age:18})# type:dict[str,int]deffunc():return10var_3func()# type:int类型注解的限制2、函数方法的类型注解# 对形参进行类型注解defadd(x:int,y:int):returnxy add(1,2)# 对返回值进行类型注解deffunc(data:list)-list:returndata3、Union类型

读完文章,也想定制专属网站?

尧图设计师 24 小时内与您沟通定制方案

免费获取报价 →
↑