定义格式
1. class ClassName: 2. <statement-1> 3. . 4. . 5. . 6. <statement-N>
类对象
- 操作方式:属性引用和实例化
1. # -*- coding: utf-8 -*- 2. # @Time : 2021/4/11 3. # @Author : 大海 4. 5. class MyClass(object): 6. i = 12345 7. 8. def test(self): 9. return f'hello world {self.i}' 10. 11. 12. if __name__ == '__main__': 13. # 属性引用所使用的标准语法: obj.name 14. print(MyClass.i) 15. print(MyClass.test) 16. # 实例化 17. x = MyClass() 18. print(x.i) 19. print(x.test)
继承
- 单继承
1. # -*- coding: utf-8 -*- 2. # @Time : 2021/4/11 3. # @Author : 大海 4. # 父类 5. class A(object): 6. def __init__(self): 7. self.num = 10 8. 9. def print_num(self): 10. print(self.num + 10) 11. 12. 13. # 子类 14. class B(A): 15. pass 16. 17. 18. if __name__ == '__main__': 19. b = B() 20. print(b.num) 21. b.print_num()
- 多继承
1. # -*- coding: utf-8 -*- 2. # @Time : 2021/4/11 3. # @Author : 大海 4. class Master(object): 5. def __init__(self): 6. self.kongfu = "古法煎饼果子配方" # 实例变量,属性 7. 8. def make_cake(self): # 实例方法,方法 9. print("[古法] 按照 <%s> 制作了一份煎饼果子..." % self.kongfu) 10. 11. def dayandai(self): 12. print("师傅的大烟袋..") 13. 14. 15. class School(object): 16. def __init__(self): 17. self.kongfu = "现代煎饼果子配方" 18. 19. def make_cake(self): 20. print("[现代] 按照 <%s> 制作了一份煎饼果子..." % self.kongfu) 21. 22. def xiaoyandai(self): 23. print("学校的小烟袋..") 24. 25. 26. class Prentice(Master, School): # 多继承,继承了多个父类(Master在前) 27. pass 28. 29. 30. if __name__ == '__main__': 31. damao = Prentice() 32. print(damao.kongfu) # 执行Master的属性 33. damao.make_cake() # 执行Master的实例方法 34. 35. # 子类的魔法属性__mro__决定了属性和方法的查找顺序 36. print(Prentice.__mro__) 37. 38. damao.dayandai() # 不重名不受影响 39. damao.xiaoyandai()
调用父类
1. # -*- coding: utf-8 -*- 2. # @Time : 2021/4/11 3. # @Author : 大海 4. class Master(object): 5. def __init__(self): 6. self.kongfu = "古法煎饼果子配方" # 实例变量,属性 7. 8. def make_cake(self): # 实例方法,方法 9. print("[古法] 按照 <%s> 制作了一份煎饼果子..." % self.kongfu) 10. 11. 12. # 父类是 Master类 13. class School(Master): 14. def __init__(self): 15. self.kongfu = "学校煎饼果子配方" 16. 17. def make_cake(self): 18. print("[学校] 按照 <%s> 制作了一份煎饼果子..." % self.kongfu) 19. super().__init__() # 执行父类的构造方法 20. super().make_cake() # 执行父类的实例方法 21. print("[学校] 按照 <%s> 制作了一份煎饼果子..." % self.kongfu) 22. 23. 24. # 父类是 School 和 Master 25. class Student(School, Master): # 多继承,继承了多个父类 26. def __init__(self): 27. super().__init__() 28. # 重写父类属性 29. self.kongfu = "学生煎饼果子配方" 30. 31. def make_cake(self): 32. self.__init__() # 执行本类的__init__方法,做属性初始化 self.kongfu = "猫氏...." 33. print("[学生] 按照 <%s> 制作了一份煎饼果子..." % self.kongfu) 34. 35. def make_all_cake(self): 36. super().__init__() # 执行父类的 __init__方法 37. super().make_cake() # 执行父类的 实例方法 38. self.make_cake() # 执行本类的实例方法 39. 40. 41. if __name__ == '__main__': 42. student = Student() 43. student.make_cake() 44. student.make_all_cake()
单例模式
1. # -*- coding: utf-8 -*- 2. # @Time : 2021/4/11 3. # @Author : 大海 4. 5. # 实例化一个单例 6. class Singleton(object): 7. __instance = None 8. __is_first = True 9. 10. def __new__(cls, age, name): 11. if not cls.__instance: 12. cls.__instance = object.__new__(cls) 13. return cls.__instance 14. 15. def __init__(self, age, name): 16. if self.__is_first: 17. self.age = age 18. self.name = name 19. Singleton.__is_first = False 20. 21. 22. if __name__ == '__main__': 23. a = Singleton(18, "我是测试小白") 24. print(id(a)) 25. print(a.age) 26. 27. b = Singleton(28, "我是测试小白") 28. 29. print(id(a)) 30. print(id(b)) 31. 32. print(a.age) 33. print(b.age) 34. 35. a.age = 19 36. print(b.age)