Python第四次笔记 对象 封装继承多态
【摘要】
初试对象
定义一个类
class Student:
name = None
gender = None
nationality = None
创建一个对象
stu_01 ...
初试对象
定义一个类
class Student:
name = None
gender = None
nationality = None
创建一个对象
stu_01 = Student()
对对象进行赋值操作
stu_01.name = "lhk"
stu_01.gender = "nan"
stu_01.nationality = "China"
类的成员方法
定义一个类,成员方法中使用成员变量的时候,需要使用self打点调用
class Student:
name = None
age = None
def say_hi(self):
print(f"Hello ,大家好,我是{self.name}")
def say_hi2(self,message):
print(f"大家好我的名字是{self.name},{message}")
stu_01 = Student()
stu_01.name = "李慧慷"
stu_01.say_hi()
stu_01.say_hi2("哎哟不错呦")
构造方法
def 5__init__(self,name,age,tel):
self.name = name
self.age = age
self.tel = tel
print("Student类创造了一个对象")
写构造方法的时候,class里面的成员变量可以不用写的
class Student:
name = None
gender = None
nationality = None
stu_01 = Student()
stu_01.name = "lhk"
stu_01.gender = "nan"
stu_01.nationality = "China"
print(stu_01.name + stu_01.gender + stu_01.nationality)
内置方法
内置方法,也就是说魔术方法,这些方法不可以直拿来用,必须在class里面进行修改
class Student:
def __init__(self,name,age):
self.name =name
self.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.age
stu1 = Student("周杰伦",31)
print(stu1)
print(str(str(stu1)))
stu2 = Student("林俊杰",36)
print(stu1 < stu2)
print(stu1 <= stu2)
print(stu1 == stu2)
封装
封装就是外面是不可以访问的,只能够内部进行访问,
5g
class Phone:
__current_voltage = 0
def __keep_singer_core(self):
print("让CPU以单核模式进行运行")
def call_by_5g(self):
if self.__current_voltage >= 1:
print("5g通话已经开启")
else:
self.__keep_singer_core()
print("电量不足,无法使用5g通话,并且已经设置为单核模式")
phone = Phone()
phone.call_by_5g()
继承
# 演示单继承
class phone:
IMEI = None
producer ="ITCAST"
def call_by_4g(self):
print("4g通话")
class phone2022(phone):
def call_by_5g(self):
print("2022年新功能:5g通话")
phone2022 = phone2022()
phone2022.call_by_4g()
# 演示多继承
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):
pass
myPhone = MyPhone()
print(myPhone.producer)
myPhone.call_by_4g()
myPhone.read_card()
myPhone.write_card()
myPhone.control()
复写父类
子类直接重写父类方法就行的
class Phone:
IMEI = None
producer ="ITCAST"
def call_by_5g(self):
print("使用5g网络进行通话")
class MyPhone(Phone):
producer = "ITHEIMA"
def call_by_5g(self):
# 方式一
print(f"父类的厂商是{Phone.producer}")
Phone.call_by_5g(self)
# 方式二
print(f"父类的厂商是{super().producer}")
super().call_by_5g()
print("子类中的5g操作")
MyPhone = MyPhone()
MyPhone.call_by_5g()
多态
# 抽象类
class Animal:
def speak(self):
pass
class Dog(Animal):
def speak(self):
print("汪汪汪")
class Cat(Animal):
def speak(self):
print("喵喵喵")
def make_noise(animal:Animal):
animal.speak()
dog = Dog()
cat = Cat()
make_noise(dog)
make_noise(cat)
多态抽象类
class AC:
def cool_wind(self):
pass
def hot_wind(self):
pass
def swing_l_r(self):
pass
class Midea_AC(AC):
def cool_wind(self):
print("美的空调核心制冷科技")
def hot_wind(self):
print("美的空调电热丝加热")
def swing_l_r(self):
print("美的空调无风感左右摇摆")
class GREE_AC(AC):
def cool_wind(self):
print("格力空调变频省电制冷")
def hot_wind(self):
print("格力空调电热丝加热")
def swing_l_r(self):
print("格力空调无风感左右摇摆")
def make_cool(ac:AC):
ac.cool_wind()
midea_ac = Midea_AC()
gree_ac = GREE_AC
make_cool(midea_ac)
make_cool(gree_ac)
文章来源: blog.csdn.net,作者:花花叔叔,版权归原作者所有,如需转载,请联系作者。
原文链接:blog.csdn.net/qq_52077949/article/details/127019353
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)