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

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

相关推荐
qq_49244844624 分钟前
Jmeter设置负载阶梯式压测场景(详解教程)
开发语言·python·jmeter
lianyinghhh42 分钟前
瓦力机器人-舵机控制(基于树莓派5)
人工智能·python·自然语言处理·硬件工程
Mike_Zhang1 小时前
python3.14版本的free-threading功能体验
python
StarPrayers.2 小时前
旅行商问题(TSP)(2)(heuristics.py)(TSP 的两种贪心启发式算法实现)
前端·人工智能·python·算法·pycharm·启发式算法
木头左2 小时前
波动率聚类现象对ETF网格密度配置的启示与应对策略
python
华仔AI智能体2 小时前
Qwen3(通义千问3)、OpenAI GPT-5、DeepSeek 3.2、豆包最新模型(Doubao 4.0)通用模型能力对比
人工智能·python·语言模型·agent·智能体
盼哥PyAI实验室2 小时前
踏上编程征程,与 Python 共舞
开发语言·python
西柚小萌新3 小时前
【深入浅出PyTorch】--6.2.PyTorch进阶训练技巧2
人工智能·pytorch·python
weixin_307779133 小时前
使用Python高效读取ZIP压缩文件中的UTF-8 JSON数据到Pandas和PySpark DataFrame
开发语言·python·算法·自动化·json