1.子类调用父类
# 大师类 object基类 class Master(object): def __init__(self): # self = Tom self.kongfu = '[大师煎饼果子配方]' # 使用自己的属性 def make_cake(self): # 徒弟类的init print(f"运用{self.kongfu}制作煎饼果子配方") # 学校类 class School(object): def __init__(self): self.kongfu = '[连锁机构煎饼果子配方]' def make_cake(self): print(f"运用{self.kongfu}制作煎饼果子配方") # 徒弟类 继承学校类和大师类 class Prentice(School, Master): def __init__(self): self.kongfu = '[独创煎饼果子配方]' def make_cake(self): # 徒弟类的init被初始化 确定徒弟类的init一定会被执行 self.__init__() print(f"运用{self.kongfu}制作煎饼果子配方") # 使用大师的配方做煎饼果子 def make_master_cake(self): # 调用大师的init函数 self = Tom Master.__init__(self) # 执行完上一行代码 self.kongfu = '[大师煎饼果子配方]' print(f"运用{self.kongfu}制作煎饼果子配方") def make_School_cake(self): School.__init__(self) print(f"运用{self.kongfu}制作煎饼果子配方") if __name__ == '__main__': # 实例化一个徒弟类的对象 Tom = Prentice() # 调用make_cake使用的是自己的配方 Tom.make_cake() Tom.make_master_cake() Tom.make_School_cake()
2.多层继承
# 大师类 object基类 class Master(object): def __init__(self): # self = Tom self.kongfu = "[大师煎饼果子配方]" def make_cake(self): print(f"运用{self.kongfu}制作煎饼果子配方") # 学校类 继承了Master class School(Master): def __init__(self): self.kongfu = "[连锁机构煎饼果子配方]" def make_cake(self): print(f"运用{self.kongfu}制作煎饼果子配方") # super() = Master 使用的是父类的方法 super().__init__() super().make_cake() # 徒弟类 继承了School类 class Prentice(School): def __init__(self): self.kongfu = "[独创煎饼果子配方]" # # 使用自己的属性 # def make_cake(self): # # 徒弟类的init被初始化 确定徒弟类的init一定会被执行 # # self.kongfu = "[独创煎饼果子配方]" self = Tom # self.__init__() # print(f"运用{self.kongfu}制作煎饼果子配方") # # # 使用大师的配方制作煎饼果子 # def make_master_cake(self): # # 调用大师的init函数 self = Tom # Master.__init__(self) # # 执行完上一行代码 self.kongfu = "[大师煎饼果子配方]" # print(f"运用{self.kongfu}制作煎饼果子配方") # # # 使用的是学校类的属性 # def make_school_cake(self): # School.__init__(self) # print(f"运用{self.kongfu}制作煎饼果子配方") def make_old_cake(self): # super() = 当前类的父类(超类) = school类 print(super()) # 调用school的init函数 super().__init__() # 调用school类的方法 super().make_cake() if __name__ == '__main__': # Jerry 是徒弟类的对象 # Prentice的父类是School,School的父类是Master Jerry = Prentice() # 调用徒弟类的方法 Jerry.make_old_cake()
3.super()超类的使用
# 大师类 object基类 class Master(object): def __init__(self): # self = Tom self.kongfu = "[大师煎饼果子配方]" def make_cake(self): print(f"运用{self.kongfu}制作煎饼果子配方") # 学校类 继承了Master class School(Master): def __init__(self): self.kongfu = "[连锁机构煎饼果子配方]" def make_cake(self): print(f"运用{self.kongfu}制作煎饼果子配方") # super() = Master 使用的是父类的方法 super().__init__() super().make_cake() # 徒弟类 继承了School类 class Prentice(School): def __init__(self): self.kongfu = "[独创煎饼果子配方]" # # 使用自己的属性 # def make_cake(self): # # 徒弟类的init被初始化 确定徒弟类的init一定会被执行 # # self.kongfu = "[独创煎饼果子配方]" self = Tom # self.__init__() # print(f"运用{self.kongfu}制作煎饼果子配方") # # # 使用大师的配方制作煎饼果子 # def make_master_cake(self): # # 调用大师的init函数 self = Tom # Master.__init__(self) # # 执行完上一行代码 self.kongfu = "[大师煎饼果子配方]" # print(f"运用{self.kongfu}制作煎饼果子配方") # # # 使用的是学校类的属性 # def make_school_cake(self): # School.__init__(self) # print(f"运用{self.kongfu}制作煎饼果子配方") def make_old_cake(self): # super() = 当前类的父类(超类) = school类 print(super()) # 调用school的init函数 super().__init__() # 调用school类的方法 super().make_cake() if __name__ == '__main__': # Jerry 是徒弟类的对象 # Prentice的父类是School,School的父类是Master Jerry = Prentice() # 调用徒弟类的方法 Jerry.make_old_cake()
4.私有属性和方法
class Prentice(): def __init__(self): self.kongfu = "[独创的煎饼果子配方]" # 加两下划线是私有属性 子类不能调用 self.__price = "2000w" def make_cake(self): print(f"用{self.kongfu}制作煎饼果子") def __getmoney(self): print("这技术不能传给你") class Tusun(Prentice): pass if __name__ == '__main__': tusun = Tusun() print(tusun.kongfu) print(tusun.__price) tusun.make_cake()
5.获取和修改私有属性
class Prentice(object): def __init__(self): self.__price = "2000万" def __get_money(self): print("这是技术不能传给你") # 普通方法 def get_money(self): # 类中的方法 可以使用类的私有属性 return self.__price def set_money(self): self.__price = "1000万" class Tusun(Prentice): pass if __name__ == '__main__': # 实例化徒孙类 对象 tusun = Tusun() print(tusun.get_money()) tusun.set_money() print(tusun.get_money())
6.多态
class Dog(object): def work(self): print("追逐敌人") class ArmyDog(): def work(self): print("指哪打哪") class DrugDog(object): def work(self): print("追击毒品") class Person(object): def work_with_dog(self, dog): # self = jc dog = ad # print(dog) dog.work() if __name__ == '__main__': # 实例化一个军犬对象 ad = ArmyDog() # 实例化一个缉毒犬对象 dd = DrugDog() # 实例化一个警察 jc = Person() # 调用函数 选择一个狗对象 和他一起工作 jc.work_with_dog(ad) # 不同的对象产生不同的反应 jc.work_with_dog(dd)
7.类属性
class Student(object): # 类属性 被所有的类和类的对象共同使用 score = 100 def __init__(self): # 实例属性 实例化对象所拥有 self.age = 18 if __name__ == '__main__': lh = Student() print(lh.score) tyf = Student() # 实例化对象调用类属性 print(tyf.score) # 类可以自己使用类属性 print(Student.score) # 类不能使用实例属性 # print(Student.age)
8.类属性的修改
class Dog(object): # 类属性 tooth = 10 if __name__ == '__main__': dh = Dog() # 实例化对象 修改类属性 dh.tooth = 12 # 看一看tooth有没有被改变 print(Dog.tooth) # 使用类 修改类属性 Dog.tooth = 14 print(Dog.tooth)