类和对象-Python-第一部分

ops/2024/9/24 6:28:24/

初识对象

使用对象组织数据

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)

类的成员方法

类的定义和使用

类的方法

成员方法的定义语法

成员变量和成员方法

注意事项

类和对象

基于类创建对象

python">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()

构造方法 

python">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)

注意方法

学生信息录入

python">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__,比较运算符实现方法

python">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__ 方法

魔术方法的总结

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

封装

私有成员

使用私有成员

python">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()

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

封装的课后习题

python">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是占位语句,用来保证函数或类定义的完整性,表示无内容,空的意思

python">#演示单继承
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)
#演示多继承下,父类成员名一致的场景

复写父类成员和调用

复写

调用父类同名成员

python">#演示单继承
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.ppmy.cn/ops/39509.html

相关文章

Java缓存caffeine使用心得

文章目录 添加依赖一、手动加载1&#xff0c;定义缓存2&#xff0c;写入缓存&#xff08;增、改&#xff09;3&#xff0c;获取大小4&#xff0c;模拟耗时操作5&#xff0c;移除缓存元素6&#xff0c;查询缓存&#xff08;查&#xff09;7&#xff0c;统计缓存 二、同步加载1&a…

CSS 定位

为什么需要浮动? 我们在访问一些网站的时候, 经常会遇到如下这种情况, 有一个组件, 一直固定在屏幕的固定位置, 无论你如何滑动这个网页, 就会固定在哪里, 如下, 下图是王者荣耀的一个官网: 要实现上面的效果, 标准流或者是浮动是无法快速实现的, 此时就需要使用定位来实现.…

三大消息传递机制区别与联系

目录 总结放开头 1、定义区别&#xff1a; EventBus Broadcast Receiver Notification 2、使用区别: EventBus Broadcast Receiver Notification 3、补充通知渠道&#xff1a; 通知渠道重要程度 总结放开头 BroadCast Receiver:属于安卓全局监听机制&#xff0c;接收…

AI“源神”启动!Llama 3发布,开闭源之争战局生变

在AI的世界里&#xff0c;开源与闭源的较量一直是科技界的热门话题。 今年年初&#xff0c;埃隆马斯克在对OpenAI及其CEO萨姆奥特曼提起诉讼时&#xff0c;就对OpenAI逐渐不公开其模型研究相关细节的行为大加谴责。“时至今日&#xff0c;OpenAI公司网站还宣称&#xff0c;它的…

MongoDB聚合运算符:$toHashedIndexKey

MongoDB聚合运算符&#xff1a;$toHashedIndexKey 文章目录 MongoDB聚合运算符&#xff1a;$toHashedIndexKey语法举例角度的双曲正切 $toHashedIndexKey计算并返回输入表达式的哈希值&#xff0c;其使用的哈希函数与MongoDB创建哈希索引相同。哈希函数将键值或字符串映射到固定…

Git命令Gitee注册idea操作git超详细

文章目录 概述相关概念下载和安装常见命令远程仓库介绍与码云注册创建介绍码云注册远程仓库操作关联拉取推送克隆 在idea中使用git集成add和commit差异化比较&查看提交记录版本回退及撤销与远程仓库关联 push从远程仓库上拉取&#xff0c;克隆项目到本地创建分支切换分支将…

JSON-server 服务的搭建

1、全局安装&#xff1a; pnpm i -g json-server2、创建db.json文件 {"posts": [{"id": 1,"title": "json-server","author": "typicode"}],"comments":[{"id": 1,"body": "…

QT day5 作业

服务器头文件 #ifndef WIDGET_H #define WIDGET_H#include <QWidget> #include <QTcpServer> //服务器类 #include <QTcpSocket> //客户端类 #include <QList> //链表类 #include <QMessageBox> //消息对话框类 #include <QDebu…