类、继承、绑定、砖石继承-super()
# !/usr/bin/python3# -*- coding: utf-8 -*-
# @Time : 2023/2/12 17:15
# @Author : xiongming
# @File : gouzhaohanshu.py
# @Desc : 构造函数、重写
class C:
def __init__(self, x, y):
#self.x 绑定类属性,右边x是传进来的参数
self.x = x
self.y = y
def add(self):
return self.x + self.y
def mul(self):
return self.x * self.y
c = C(2, 3)
c.add()
c.mul()
c.__dict__
# 重写
class D(C):
def __init__(self, x, y, z):
C.__init__(self, x, y)
self.z = z
def add(self):
return C.add(self) + self.z
def mul(self):
return C.mul(self) + self.z
d = D(2, 3, 4)
d.add()
d.mul()
# 砖石继承 A-B1/B2-C,super()解决重复
# Method\Resolution\Order 方法mro()、解析__mro__、顺序
class A():
def __init__(self):
print("哈喽,我是A~")
class B1(A):
def __init__(self):
super().__init__()
print("哈喽,我是B1")
class B2(A):
def __init__(self):
super().__init__()
print("哈喽,我是B2")
class C(B1, B2):
def __init__(self):
super().__init__()
print("哈喽,我是C")
c = C()
C.mro()
页:
[1]