单和多继承属性查找顺序,super,多态,组合
2022/2/16 23:16:34
本文主要是介绍单和多继承属性查找顺序,super,多态,组合,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
单继承下的属性查找顺序
# class Foo: # def f1(self): # print('from Foo.f1') # # def f2(self): # print('from Foo.f2') # self.f1() # # # class Bar(Foo): # def f1(self): # print('from Bar.f1') # # # 属性查找的顺序:现在对象自己的名称空间中查找,在去产生对象的类中查找,在去继承的类中查找 # # obj = Bar() # obj.f2() class Foo: def __f1(self): # _Foo__f1() print('from Foo.f1') def f2(self): print('from Foo.f2') self.__f1() # _Foo__f1() class Bar(Foo): def __f1(self): # _Bar__f1() print('from Bar.f1') # 属性查找的顺序:现在对象自己的名称空间中查找,在去产生对象的类中查找,在去继承的类中查找 obj = Bar() obj.f2()
多继承下的属性查找
# 经典类:深度优先查询 # 新式类:广度优先查询 class G(object): # def test(self): # print('from G') pass class E(G): # def test(self): # print('from E') pass class B(E): # def test(self): # print('from B') pass class F(G): # def test(self): # print('from F') pass class C(F): # def test(self): # print('from C') pass class H(): # def test(self): # print('from C') pass class D(H): # def test(self): # print('from D') pass class A(B, C, D): # def test(self): # print('from A') pass f1 = A() f1.test()
super和mro列表
# class People(): # school = 'SH' # # def __init__(self, name, age, gender): # self.name = name # self.age = age # self.gender = gender # # # class Student(People): # def __init__(self, name, age, gender, course=None): # if course is None: # course = [] # # People.__init__(self, name, age, gender) # 指名道姓的调用 # # super(Student, self) 返回的是一个特殊的对象 # # super(Student, self).__init__(name, age, gender) # python2中的写法 # super().__init__(name, age, gender) # python3中的写法 # self.course = course # # def choose_course(self): # print('aaaa') # # stu = Student('tom', 19, 'male') # print(stu.school) # class Teacher(People): # def __init__(self, name, age, gender, level): # People.__init__(self, name, age, gender) # self.level = level # # def score(self): # print('打分') class A(): def test(self): super().test() class B: def test(self): print('from B') class C(A, B): pass c = A() c.test()
多态与多态性
多态:同一种事物的多种形态 水:液态,固态,气态 动物:人,猪,狗,猫... Animal类已经变成抽象类了, 抽象类的特点:只能被继承,不能被实例化 class Animal(metaclass=abc.ABCMeta): @abc.abstractmethod def speak(self):pass class People(Animal): def jiao(self): pass def speak(self): pass class Pig(Animal): def speak(self): pass class Dog(Animal): def speak(self): pass obj1 = People() obj1.speak()
组合
# 组合:一个对象拥有一个属性,该属性的值是另外一个对象 继承:满足什么是什么的关系 组合:满足什么有什么的关系 class People(): school = 'SH' def __init__(self, name, age, gender): self.name = name self.age = age self.gender = gender class Course(): def __init__(self, name, period, price): self.name = name self.period = period self.price = price python=Course('python', '6mon', 30000) linux=Course('linux', '5mon', 20000) # print(python.name) # print(python.period) # print(python.price) class Student(People): def __init__(self, name, age, gender, course=None): if course is None: course = [] # People.__init__(self, name, age, gender) # 指名道姓的调用 # super(Student, self) 返回的是一个特殊的对象 # super(Student, self).__init__(name, age, gender) # python2中的写法 super().__init__(name, age, gender, ) # python3中的写法 self.course = course def choose_course(self): print('aaaa') stu = Student('tom', 19, 'male') stu.course.append(python) stu.course.append(linux) for i in stu.course: print(i.name)
这篇关于单和多继承属性查找顺序,super,多态,组合的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2025-01-102025 蛇年,J 人直播带货内容审核团队必备的办公软件有哪 6 款?
- 2025-01-10高效运营背后的支柱:文档管理优化指南
- 2025-01-10年末压力山大?试试优化你的文档管理
- 2025-01-10跨部门协作中的进度追踪重要性解析
- 2025-01-10总结 JavaScript 中的变体函数调用方式
- 2025-01-10HR团队如何通过数据驱动提升管理效率?6个策略
- 2025-01-10WBS实战指南:如何一步步构建高效项目管理框架?
- 2025-01-10实现精准执行:团队协作新方法
- 2025-01-10如何使用工具提升活动策划团队的工作效率?几个必备工具推荐
- 2025-01-10WiX 标签使用介绍:打造专业安装程序的利器