继承
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()
覆盖+重写父类函数
class Pet:def __init__(self,name):self.name=nameprint(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=ageself.sex=sexdef eat(self):super().eat()#利用继承中的super()打点调用父类函数print(f'{self.name}吃完东西后用唾液洗洗脸')
c1=Cat("大橘",17,"女孩纸")
c1.eat()
多层继承
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 爆金币
我有钱!
子类调用父类时,调用对象的顺序是深度优先
class GrandFather:def getMoney(self):print("爷爷给了零花钱....")class Father(GrandFather):passclass Mother:def getMoney(self):print("母亲给了零花钱....")# 继承Father和Mother
class Child(Father, Mother):def getMoney(self):super().getMoney()print("孩子有了零花钱.....")c1=Child()
c1.getMoney()
print(Child.mro())#子类的调用顺序
爷爷给了零花钱....
孩子有了零花钱.....
[<class '__main__.Child'>, <class '__main__.Father'>, <class '__main__.GrandFather'>, <class '__main__.Mother'>, <class 'object'>]
初始化是广搜
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中可以直接调用
即便两个类之间没有继承关系,也可以调用
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条件较为严格
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博客
静态:(打印或者是绘图)不需要创建对象的时候可以使用
# 静态方法:用@staticmethod装饰的不带self参数的方法叫做静态方法
# 类的静态方法可以没有参数,可以直接使用类调用class Dog:@staticmethoddef bark():#这里没有selfprint("wangwang!")d = Dog()
d.bark()
Dog.bark()
静态方法不能通过self和类名访问成员变量
类方法:
class Dog:legs = 4teeth = 42# 类方法@classmethoddef printInfo(cls):print(f"狗有{cls.legs}条腿,{cls.teeth}颗牙齿")d = Dog()
d.printInfo()
Dog.printInfo()
两者均可以使用类名和对象名打点调用函数