# 一、类
# 1.设计一个类:一张登记表
class Student:
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 = "四川省"
# 4.获取对象中记录的信息
print(stu_1.name)
print(stu_1.gender)
print(stu_1.nationality)
print(stu_1.native_place)
# 二、成员方法
class Student:
name = None
def say_hi(self):
print(f"大家好呀,我是{self.name}")
def say_hi2(self, msg):
print(f"大家好,我是:{self.name}, {msg}")
stu1 = Student()
stu1.name = "吉吉国王"
stu1.say_hi2("hahaha")
stu2 = Student()
stu2.name = "李华"
stu2.say_hi2("啊哈哈哈")
# 三、面向对象编程
# 1. 创建一个闹钟类
class Clock:
id = None
price = None
def ring(self):
import winsound
winsound.Beep(2000, 3000)
# 2. 创建闹钟对象让其工作
clock1 = Clock()
clock1.id = "003032"
clock1.price = 19.99
print(f"闹钟ID{clock1.id},价格{clock1.price}")
clock1.ring()
# 四、构造方法:__init__方法
class Student:
name = None # 成员方法可不写
age = None
tel = None
def __init__(self, name, age, tel):
self.name = name
self.age = age
self.tel = tel
print("Student类创建了一个类对象")
stu1 = Student("吉吉国王", 31, 110)
print(stu1.name)
print(stu1.age)
print(stu1.tel)
# 五、魔术方法:内置方法(如__init__方法)
class Student:
def __init__(self, name, age):
self.name = name
self.age = age
# 1. 字符串方法:__str__
def __str__(self):
return f"Student类对象,name:{self.name},age:{self.age}"
# 2. 大小比较:__lt__
def __lt__(self, other):
return self.age < other.age
# 3. <=,>=比较:__le__
def __le__(self, other):
return self.age <= other.age
# 4. 相等判断:__eq__
def __eq__(self, other):
return self.age == other.age
stu1 = Student("吉吉国王", 21)
stu2 = Student("吉吉国王", 25)
print(stu1)
print(str(stu1))
print(stu1 < stu2)
print(stu1 > stu2)
print(stu1 >= stu2)
print(stu1 == stu2)
# 六、封装
# 1.私有成员和私有方法
class Phone:
__current_voltage = None # 手机运行电压
def __kepp_single_core(self):
print("让CPU以单核运行")
phone =Phone()
phone.__keep_single_core # 均无法使用
peint(phone.__current_voltage)
# 但是可以在类对象内部使用
class Phone:
__current_voltage = 0.1 # 手机运行电压
def __kepp_single_core(self):
print("让CPU以单核运行")
def call_by_5G(self):
if self.__current_voltage >= 1:
print("5G通话已开启")
else:
self.__kepp_single_core()
print("电量不足,无法使用5G通话,并设置为单核运行")
phone = Phone()
phone.call_by_5G()
# 案例
class Phone:
__is_5G_enable = False
def __cheak_5G(self):
if self.__is_5G_enable:
print("5G开启")
else:
print("5G关闭,使用4G")
def call_by_5G(self):
self.__cheak_5G()
print("正在通话中")
phone = Phone()
phone.call_by_5G()
# 七、继承
# 1. 单继承
class Phone:
IMEI = None
producer = "Xiaomi"
def call_by_4G(self):
print("4G通话")
class Phone2024(Phone): # 新功能
face_id = "10001"
def call_by_5G(self):
print("5G通话")
phone = Phone2024()
print(phone.producer)
phone.call_by_4G()
phone.call_by_5G()
# 2. 多继承
class NFC:
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, NFC, RemoteControl):
pass # pass关键字用于补充语法
phone = MyPhone()
phone.call_by_4G()
phone.read_card()
phone.write_card()
phone.control()
# 同名成员,左边优先级最高
print(phone.producer)
Python进阶:一、面向对象编程1(对象、成员变量、成员方法、魔术方法、封装、继承)
最新推荐文章于 2025-12-18 10:59:28 发布
1181

被折叠的 条评论
为什么被折叠?



