当前位置: 首页 > news >正文

类和对象-Python-第一部分

初识对象

使用对象组织数据

class Student:name=Nonegender=Nonenationality=Nonenative_place=Noneage=Nonestu_1=Student()stu_1.name="林军杰"
stu_1.gender="男"
stu_1.nationality="中国"
stu_1.native_place="山东"
stu_1.age=31print(stu_1.name)
print(stu_1.gender)
print(stu_1.nationality)
print(stu_1.native_place)
print(stu_1.age)

类的成员方法

类的定义和使用

类的方法

成员方法的定义语法

成员变量和成员方法

注意事项

类和对象

基于类创建对象

class Clock:id=Noneprice=Nonedef ring(self):import winsoundwinsound.Beep(2000,3000)#2000是响铃频率,3000是响铃时间clock1=Clock()
clock1.id="003032"
clock1.price=19.19
print(f"闹钟ID:{clock1.id},价格:{clock1.price}")
clock1.ring()clock2=Clock()
clock2.id="003033"
clock2.price=21.99
print(f"闹钟ID:{clock2.id},价格:{clock2.price}")
clock2.ring()

构造方法 

class Student:name=Noneage=Nonetel=Nonedef __init__(self,name,age,tel):self.name=nameself.age=ageself.tel=telprint("Student类创建了一个类对象")stu=Student("周杰轮",31,1850000666)
print(stu.name)
print(stu.age)
print(stu.tel)

注意方法

学生信息录入

class Student:def __init__(self,name,age,address):self.name=nameself.age=ageself.address=addressfor i in range(1,11):Name=input("输入学生姓名")Age=input("输入学生年龄")Address=input("输入学生住址")stu =Student(Name,Age,Address)print(f"学生{i}信息录入完成,信息为:{stu.name},{stu.age},{stu.address}")

魔术方法

__str__字符串

__le__小于等于比较符号方法

__eq__,比较运算符实现方法

class Student:def __init__(self,name,age):self.name=nameself.age=age# __str__魔术方法      #当需要把类的对象变成字符串使用def __str__(self):return f"Student类对象,name:{self.name},age:{self.age}"#  __lt__魔术方法def __lt__(self, other):return self.age<other.age#__le__魔术方法def __le__(self, other):return self.age <= other.age#__eq__魔术方法def __eq__(self, other):return self.age == other.agestu1=Student("周杰轮",31)
stu2=Student("林俊节",36)
print(stu1)
#输出的是内存地址print(stu1<stu2)#  __lt__魔术方法
print(stu1>stu2)print(stu1 <= stu2)  # 使用 __le__ 方法
print(stu1 >= stu2)print(stu1 == stu2)  # 使用 __eq__ 方法

魔术方法的总结

注:面相对象三大特性:封装,继承,多态

封装

私有成员

使用私有成员

class Phone:__current_voltage=1      #当前手机运行电压def __keep_single_core(self):print("让CPU以单核模式运行")def call_by_5g(self):if self.__current_voltage>=1:print("5g通话已开启")else:self.__keep_single_core()print("电量不足,无法使用5g通话,并设置为单核运行进行省电")Phone=Phone()
#Phone.__keep_single_core()
#print(Phone.__keep_single_core())  wrong
Phone.call_by_5g()

注:私有成员在类中提供仅供内部使用的属性和方法,而不对外开放(类对象无法使用)

封装的课后习题

class Phone:__is_5g_enable=Falsedef __check_5g(self):if self.__is_5g_enable:print("5g开启")else:print("5g关闭,使用4g网络")def call_by_5g(self):self.__check_5g()print("正在通话中")phone=Phone()
phone.call_by_5g()

继承的基础语法

单继承

多继承

多继承注意事项

注:pass关键字,pass是占位语句,用来保证函数或类定义的完整性,表示无内容,空的意思

#演示单继承
class Phone:IMEI=None   #序号producer="ITCast"   #厂商def call_by_4g(self):print("4g通话")class Phone_2022(Phone):face_id="10001" #面部识别IDdef call_by_5g(self):print("2024年新功能,5g通话")phone=Phone_2022()
print(Phone.producer)
phone.call_by_4g()
phone.call_by_5g()#演示多继承
class NFCReader:nfc_type="第五代"producer="HM"def read_card(self):print("NFC读卡")def write_card(self):print("NFC写卡")class RemoteControl:rc_type="红外遥控"def control(self):print("红外遥控开启了")class MyPhone(Phone,NFCReader,RemoteControl):passphone=MyPhone()
phone.call_by_4g()
phone.read_card()
phone.write_card()
phone.control()
print(phone.producer)
#演示多继承下,父类成员名一致的场景

复写父类成员和调用

复写

调用父类同名成员

#演示单继承
class Phone:IMEI=None   #序号producer="ITCast"   #厂商def call_by_5g(self):print("使用5g网络进行通话通话")#演示多继承下,父类成员名一致的场景
class MyPhone(Phone):producer="ITHEIMA"def call_by_5g(self):print("开启CPU单核模式,确保通话时省电")#方式1# print(f"父类的厂商是{Phone.producer}")# Phone.call_by_5g(self)#方式2print(f"父类的厂商是:{super().producer}")super().call_by_5g()print("使用5g网络进行通话")print("关闭CPU单核运行模式,确保性能")phone=MyPhone()
phone.call_by_5g()
print(phone.producer)

 若有侵权,请联系作者

http://www.lryc.cn/news/342165.html

相关文章:

  • Pytorch实现图片异常检测
  • 【NOI-题解】1586. 扫地机器人1430 - 迷宫出口1434. 数池塘(四方向)1435. 数池塘(八方向)
  • 探究MySQL行格式:解析DYNAMIC与COMPACT的异同
  • MATLAB绘制蒸汽压力和温度曲线
  • repo跟git的关系
  • Mysql 8.0 -- 最新版本安装(保姆级教程)
  • sql优化思路
  • gin学习1-7
  • likeshop多商户单商户商城_likeshop跑腿源码_likeshop物品租赁系统开源版怎么配置小程序对接?
  • (done) LSTM 详解 (包括它为什么能缓解梯度消失)
  • springboot使用研究
  • 老旧房屋用电线路故障引起的电气火灾预防对策​
  • OpenAI发布GPT-4.0使用指南
  • 【WEEK11】学习目标及总结【Spring Boot】【中文版】
  • Unity 性能优化之图片优化(八)
  • C++类细节,面试题02
  • Stylus的引入
  • 前端框架-echarts
  • 【StarRocks系列】 Trino 方言支持
  • 【2024最新华为OD-C卷试题汇总】URL拼接 (100分) - 三语言AC题解(Python/Java/Cpp)
  • 【ARM 嵌入式 C 字符串系列 23.7 -- C 实现函数 isdigit 和 isxdigit】
  • 三分钟了解计算机网络核心概念-数据链路层和物理层
  • 数据结构===堆
  • AAA、RADIUS、TACACS、Diameter协议介绍
  • Nacos高频面试题及参考答案(2万字长文)
  • CMakeLists.txt语法规则:条件判断中表达式说明四
  • Hive概述
  • buuctf-misc-33.[BJDCTF2020]藏藏藏1
  • golang 基础知识细节回顾
  • 递归陷阱七例