上次总结了python中面向对象的特性之一 —— 封装,这回总结梳理一下另一特性 —— 继承。继承顾名思义就是子类获取父类的一些属性、方法。但是有一点需要注意的就是,在继承的时候一般只能继承父类中的非私有属性以及方法。具体内容如下。
1. 继承
(1)继承的格式:
class 子类名(父类名):
子类中的代码
(2)继承是指,子类从父类继承父类中的非私有属性和方法
class Fu:
def __init__(self):
self.house = "5套房子"
self.gold = "2箱黄金"
def run_company(self):
print("运营公司...")
class Zi(Fu):
pass#####只有这么写才可以进行下面的操作,否则需要super操作
# 创建子类对象
zi = Zi()
# 通过子类对象调用从父类中继承过来的属性和方法
zi.run_company()
print(zi.gold)
print(zi.house)
2. 重写父类中的方法:就是在子类中重新定义一个和父类同名的方法,在通过子类创建对象调用该方法时,其实是调用子类中的方法,而不是弗雷总得方法
如果要调用父类中的方法则需要使用super操作
class Fu:
def __init__(self):
self.house = "5套房子"
self.gold = "2箱黄金"
def run_company(self):
print("运营公司...")
class Zi(Fu):
def run_company(self):
print("每个班级放两个程序猿鼓励师..")
print("运营公司...")
def call_super(self):
# 调用父类中被重写的方法
super().run_company()
# 创建子类对象
zi = Zi()
# 通过子类对象调用重写后的方法:调用的是子类中重写后的方法,不是父类中的方法
# zi.run_company()
zi.call_super()##这个是通过在子类中定义一个新的方法来调用父类中的方法
也可以在子类中写属于子类自己的方法,这些方法可直接通过子类的对象直接调用,如下:
class Zi(Fu):
def find_girl(self):
print("找一个网红的女朋友...")
self.girl_friend = "网红小姐姐"
def play_game(self):
print("玩游戏....")
# 创建子类对象
zi = Zi()
#调用子类中自己的方法
zi.find_gir()
zi.play_game()
3. 多层继承:指的是一层一层的继承,简单没什么用
"""
多层继承:一层一层的继承:
"""
class A:
def __init__(self):
self.a = 100
def show_a(self):
print(self.a)
class B(A):
def __init__(self):
self.b = 200
def show_b(self):
print(self.b)
class C(B):
def __init__(self):
self.c = 300
def show_c(self):
print(self.c)
###C类中既有A又有B 的属性和方法
4. 在子类中调用父类中的方法。
(1)当子类中没有定义自己的属性时,可以有以下几种调用方式
class A:
def __init__(self):
self.a = 100
def show(self):
print(self.a)
class B(A):
def show1(self):
A.show(self)###父类名.方法名(self)
super().show()###super().方法名()
self.show()###self.方法名()
b = B()
b.show1()
b.show()#直接通过子类对象调用父类的方法名也可以
(2)当子类中了利用init方法定义了自己的属性时,运用下面的调用方式
class A:
def __init__(self):
self.a = 100
def show(self):
print(self.a)
class B(A):
def __init__(self):
super().__init__()#####需要在这儿使用super()方法,否则下面几种方式都不能用##调用父类属性也是同样的道理
self.b = 200
def show1(self):
A.show(self)###父类名.方法名(self)
super().show()###super().方法名()
self.show()###self.方法名()
b = B()
b.show1()
b.show()
总结一下:在子类中没有重写init方法时,直接用上述几种格式调用即可,但是一旦子类中重写了父类中的init方法,则需要利用super关键字,原因是:重写父类中的__init__方法,当创建子类对象时,解析器会自动调用子类中的__init__方法不会调用父类中这个方法.那么父类__init__方法中添加属性,就不能继承过来了,子类中就没有这些属性了.。
########注:super关键字只能在子类内使用,而不能在子类外面使用
5. 在子类中调用父类中的私有属性和方法
class Fu:
def __init__(self):
self.house = "5套房子"
self.gold = "2箱黄金"
# 私有属性
self.__mishu = "1号秘书"
# 私有方法
def __find_mishu(self):
print("找秘书...")
# 定义一个公共的方法,在里面方法私有属性和私有方法
def test(self):
# 访问私有属性
print("秘书:"+self.__mishu)
self.__find_mishu()
class Zi(Fu):
def __init__(self):
# 调用父类中的__init__方法
super().__init__()########这步就是上面4(2)中说的添加一个super关键字
self.girl_friend = "网红小嘎妹"
# 创建子类对象
zi = Zi()
# 通过test方法间接访问父类中的私有属性和私有方法
zi.test()
总结一下:要想通过子类调用父类中的私有属性和私有方法,不能直接调用,需要在父类中创建一个中间方法如test方法
在这个中间方法中调用私有属性和方法,然后通过子类创建的对象调用该中间方法
6. 多继承:就是是一个子类继承多个父类
多继承的格式:
class 子类名(父类名1,父类名2,...):
子类的代码
class A:
def __init__(self,a):
self.a = a
def show_a(self):
print(self.a)
def method01(self,name):
print("我是A类中的method01方法..."+name)
class B:
def __init__(self):
self.b = 200
def show_b(self):
print(self.b)
def method01(self,name):
print("我是B类中的method01方法..." + name)
class C(A,B):
def __init__(self):
A.__init__(self,100)
B.__init__(self)
def show_c(self):
# 在子类中调用方指定父类A类中的method01方法
A.method01(self,"张三")
# 在子类中调用方指定父类B类中的method01方法
B.method01(self,"李四")
c = C()
c.show_a()
c.show_b()
print(c.a)
print(c.b)
c.show_c()
总结一下:在子类继承多个父类时,在子类的init方法中,super关键字已经失效了,只能机场多个父类中的一个。
需要用:父类名.init关键字(self,参数)来继承,这样操作完之后才能正常调用父类中的方法,当然还有另外一种,但是比较麻烦,就先不总结了。
能力有限,请多多指教。