Python | 刷题笔记

继承

python 复制代码
class Father:
    __secret="you are your own kid"
    stroy="i'am a handsome boy..."
    def  tellstory(self):
        print("我的故事:",self.stroy)
    def  __tellstory(self):
        print("我的秘密:",Father.__secret)
class Son(Father):
    def tell(self):
        Father._Father__tellstory(self)#调用父类的私有函数 父类名打点_父类名__私有函数名(self)
        self.tellstory()
s1=Son()
s1.tell()

覆盖+重写父类函数

python 复制代码
class Pet:
    def  __init__(self,name):
        self.name=name
        print(f"一个名叫{self.name}的宠物出生了")
    def eat(self):
        print(f'{self.name}在吃东西...')
class Dog(Pet):
    def lookAfter(self):
        print(f'{self.name}在看门')
    def eat(self):
        print(f'{self.name}在啃骨头')
class Cat(Pet):
    def __init__(self,name,age,sex):
        super().__init__(name)#利用父类自带的函数初始化
        self.age=age
        self.sex=sex
    def eat(self):
        super().eat()#利用继承中的super()打点调用父类函数
        print(f'{self.name}吃完东西后用唾液洗洗脸')
c1=Cat("大橘",17,"女孩纸")
c1.eat()

多层继承

python 复制代码
class Father:
    def getq(self):
        print("father 爆金币")
class Monther:
    def getq(self):
        print("monther 爆金币")
class Child(Father,Monther):
    def getq(self):
        super().getq()
        print("我有钱!")
c1=Child()
c1.getq()

father 爆金币
我有钱!

子类调用父类时,调用对象的顺序是深度优先

python 复制代码
class GrandFather:
    def getMoney(self):
        print("爷爷给了零花钱....")


class Father(GrandFather):
    pass


class Mother:
    def getMoney(self):
        print("母亲给了零花钱....")


# 继承Father和Mother
class Child(Father, Mother):
    def getMoney(self):
        super().getMoney()
        print("孩子有了零花钱.....")

c1=Child()
c1.getMoney()
print(Child.mro())#子类的调用顺序

爷爷给了零花钱....

孩子有了零花钱.....

\, \, \, \, \

初始化是广搜

python 复制代码
class Human():
    def __init__(self):
        print("人类...")
class Father(Human):
    def __init__(self):
        print("父亲开始初始化...")
        super().__init__()
        print("父亲初始化结束...")
class Monther(Human):
    def __init__(self):
        print("母亲开始初始化...")
        super().__init__()
        print("母亲初始化结束...")
class Child(Father,Monther):
    def __init__(self):
        print("孩子开始初始化...")
        super().__init__()
        print("孩子初始化结束...")
c1=Child()

孩子开始初始化...

父亲开始初始化...

母亲开始初始化...

人类...

母亲初始化结束...

父亲初始化结束...

孩子初始化结束...


多态

这里传入的是v,在调用animallEating时需要调用eating对象
而venusFlaytrap类也具有对象eating,在python中可以直接调用
即便两个类之间没有继承关系,也可以调用

python 复制代码
class Animal:
    def eating(self):
        print("动物下在吃东西.....")


class Pet(Animal):
    def eating(self):
        print("宠物在吃东西.....")


class Dog(Pet):
    def eating(self):
        print("狗在啃骨头.....")


class Cat(Pet):
    def eating(self):
        print("猫在吃鱼....")


class Zoo:
    def animallEating(self, animal):
        if isinstance(animal, Animal):
            print("这是展示动物吃东西的地方:")
        else:
            print("这是非动物吃饭的展示")
        animal.eating()


class venusFlytrap:
    def eating(self):
        print("捕蝇草在吃小虫子.....")


v = venusFlytrap()
z = Zoo()
# z是Zoo类但是可以调用venusFlytrap类的对象
z.animallEating(v)

type isinstance异同

isinstance 会判断父类

type条件较为严格

python 复制代码
c=Cat()
#判断类型时,只看直接类型
#type是严格满足类型
print(type(c) is Cat) #True
print(type(c) is Animal) #False
print(type(c) is Pet) #False


#isinstance 判断对象是否为一个类型的实例
# 判断实例类型时,涵盖父类的类型
print("*"*30)
print(isinstance(c,Cat)) #True
print(isinstance(c,Pet)) #True
print(isinstance(c,Animal)) #True
print(isinstance(v,Animal)) #False

静态方法和类方法

Python------类方法和静态方法_python类方法和静态方法-CSDN博客

静态:(打印或者是绘图)不需要创建对象的时候可以使用

python 复制代码
# 静态方法:用@staticmethod装饰的不带self参数的方法叫做静态方法
#        类的静态方法可以没有参数,可以直接使用类调用

class Dog:
    @staticmethod
    def bark():#这里没有self
        print("wangwang!")


d = Dog()
d.bark()
Dog.bark()

静态方法不能通过self和类名访问成员变量

类方法:

python 复制代码
class Dog:
    legs = 4
    teeth = 42

    # 类方法
    @classmethod
    def printInfo(cls):
        print(f"狗有{cls.legs}条腿,{cls.teeth}颗牙齿")


d = Dog()
d.printInfo()
Dog.printInfo()

两者均可以使用类名和对象名打点调用函数

相关推荐
TomEval8 小时前
【测AI】第26篇:线上模型监控与告警体系 — 测试不是终点
人工智能·python·功能测试·ai
prince_zxill9 小时前
PEP 751 pylock.toml 实测:pip 26.3 装 6 个包版本全钉死,但 [web] 选不中任何东西
python·pip·依赖管理·pep 751
Thanks_ks11 小时前
Python 的垃圾回收——引用计数撑起日常,分代回收收拾残局
python·内存管理·垃圾回收·循环引用·引用计数·gc 模块·weakref
CyrusYiVPP11 小时前
实测 uv 0.13.7:同一套包,pip 装 94 秒,它只用 19 秒
人工智能·python
yiangL_11 小时前
Python 学习笔记:递归、turtle绘图、字符串与函数
笔记·python·学习
lula88812 小时前
PVC-U、PVC-C、PVC-O 差一个字母,差的是三种增强路线
python
数字工作台14 小时前
给 LangGraph 加一道可审计记忆:替换默认 Checkpointer,实现防篡改 + 崩溃不丢数据
python·agent
DKPT14 小时前
Python 的 Web 框架:选型选错的人,都踩了同一个坑
python
2401_8685347815 小时前
逻辑网络设计
网络·python
2601_9679791215 小时前
python如何组织文件?python文件操作全介绍!
python·文件操作·压缩文件·遍历文件夹·shutil模块