前几天更新了,关于Python面向对象的基础部分,今天带给大家的是继承这一部分的知识点。
讲到国民老公,大家都会想到汪四琮(不想避免误会,用谐音化名)。他有个有钱的父亲叫汪剑岭,总所周知他父亲旗下有千达这个地产公司,几乎遍布了整个中国。他的母亲可能很少人知道,叫凌玲是凌式国际投资银行的老板。
我就用他们的这个例子来讲讲Python继承这一块:
先讲讲单继承:
按照中国的传统,长子要继承父亲的财产
class Jianling_WANG(object):
def __init__(self):
self.money = "我有千达"
def make_money(self):
print("我的钱来自:", self.money)
class Sicong_WANG(Jianling_WANG):
pass
Fuerdai = Sicong_WANG()
Fuerdai.make_money()
然后就是多继承:
幸运的汪四琮不但有父亲的财产还有母亲的财产:
class Jianling_WANG(object):
def __init__(self):
self.money = "我有千达"
def make_money(self):
print("我的钱来自:", self.money)
class Ning_Ling(object):
def __init__(self):
self.money = "我有凌氏国际投资银行"
def make_money(self):
print("我的钱来自:", self.money)
class Sicong_WANG(Jianling_WANG, Ning_Ling):
pass
Fuerdai = Sicong_WANG()
Fuerdai.make_money()
反正就看汪四琮怎么选吧
但是一向倔强的汪四琮也有自己的产业:
class Jianling_WANG(object):
def __init__(self):
self.money = "我有千达"
def make_money(self):
print("我的钱来自:", self.money)
class Ning_Ling(object):
def __init__(self):
self.money = "我有凌氏国际投资银行"
def make_money(self):
print("我的钱来自:", self.money)
class Sicong_WANG(object):
def __init__(self):
self.money = "我有IG"
def make_money(self):
print("我的钱来自:", self.money)
Fuerdai = Sicong_WANG()
Fuerdai.make_money()
但是偶尔他也会亏钱,怎么办呢,还得靠他父母:
class Jianling_WANG(object):
def __init__(self):
self.money = "我有千达"
def make_money(self):
print("我的钱来自:", self.money)
class Ning_Ling(object):
def __init__(self):
self.money = "我有凌氏国际投资银行"
def make_money(self):
print("我的钱来自:", self.money)
class Sicong_WANG(object):
def __init__(self):
self.money = "我有IG"
def make_money(self):
print("我的钱来自:", self.money)
def make_money_from_Jianling_WANG(self):
Jianling_WANG.__init__(self)
Jianling_WANG.make_money(self)
def make_money_from_Ling_LI(self):
Ning_Ling.__init__(self)
Ning_Ling.make_money(self)
Fuerdai = Sicong_WANG()
Fuerdai.make_money()
Fuerdai.make_money_from_Jianling_WANG()
Fuerdai.make_money_from_Ling_LI()
当然靠父母的方式有很多这种太麻烦了,就得用super()
```python
class Jianling_WANG(object):
def __init__(self):
self.money = "我有千达"
def make_money(self):
print("我的钱来自:", self.money)
class Ning_Ling(object):
def __init__(self):
self.money = "我有凌氏国际投资银行"
def make_money(self):
print("我的钱来自:", self.money)
class Sicong_WANG(Jianling_WANG):
def __init__(self):
self.money = "我有IG"
def make_money(self):
print("我的钱来自:", self.money)
def make_money_from_Jianling_WANG(self):
super().__init__()
super().make_money()
Fuerdai = Sicong_WANG()
Fuerdai.make_money()
Fuerdai.make_money_from_Jianling_WANG()
他想靠谁,只要在:
class Sizong_WANG(self),self这里把名字改成他爸或者他妈
然后调用super()
当然如果胆子够大,我全都要:
class Jianling_WANG(object):
def __init__(self):
self.money = "我有千达"
def make_money(self):
print("我的钱来自:", self.money)
class Ning_Ling(Jianling_WANG):
def __init__(self):
self.money = "我有凌氏国际投资银行"
def make_money(self):
print("我的钱来自:", self.money)
class Sicong_WANG(Ning_Ling):
def __init__(self):
self.money = "我有IG"
def make_money(self):
print("我的钱来自:", self.money)
def make_money_from_Parents(self):
super().__init__()
super().make_money()
Fuerdai = Sicong_WANG()
Fuerdai.make_money()
Fuerdai.make_money_from_Parents()
按照中国传统,家庭地位一般都是-----> 父亲、母亲、孩子
所以只要在每个类的(self)写上名字就可以了
私有属性
汪剑岭发现当他的儿子继承了他所有的财产的时候,自己的晚年生活该怎么办?
所以就要给自己留一条后路,这个是他儿子拿不到的
class Jianling_WANG(object):
def __init__(self):
self.money = "我有千达"
self.__power=10000000
def get_power(self):
return self.__power
def make_money(self):
print("我的钱来自:", self.money)
class Sicong_WANG(Jianling_WANG):
pass
Fuerdai=Sicong_WANG()
print(Fuerdai.get_power())
如果没有get_power()这个函数,这个self._power是拿不到的。
我觉得可能面向对象 还得再讲两次才能全部讲完。然后还得再讲一次Python的数据类型还有基本函数。