目录
一、私有和公有属性的定义和使用
1.公有属性定义和使用
2.私有属性
二、继承
1.应用
2.子类不能用父类的私有方法
3.子类初始化父类
4.子类重写和调用父类方法
5.多层继承
6.多层继承-初始化过程
7.多继承基本格式
8.多层多继承时的初始化问题
9.多继承初始化传参问题
10.super简化写法
11.影响mro的顺序
12.调用父类两种方法
结语
一、私有和公有属性的定义和使用
1.公有属性定义和使用
公有属性会破坏封装性
class Account(object):
def __init__(self,name,balance):
self.name = name
self.balance = balance
def show_info(self):
print(self.name + '有' + self.balance,"元")
jack = Account('JackMa',9999999)
print(jack.name)
print(jack.balance)
jack.name = '郭'
print(jack.name)
print(jack.balance)
class Account(object):
def __init__(self,name,balance):
self.name = name
self.balance = balance
def show_info(self):
print(self.name + '有' + self.balance,"元")
jack = Account('JackMa',9999999)
print(jack.name)
print(jack.balance)
jack.name = '郭'
print(jack.name)
print(jack.balance)
2.私有属性
当一个类中的所有属性或方法,全是私有的时候,这个类时无意义的
class Account(object):
def __init__(self,name,balance):
self.__name = name
self.__balance = balance
def show_info(self):
print(self.__name+'有',self.__balance,'元')
jack = Account('Jackma',99999999)
jack.show_info()
私有属性set和get方法
class Account(object):
def __init__(self,name,balance):
self.__name = name
self.__balance = balance
def show_info(self):
print(self.__name+'有',self.__balance,'元')
def get_name(self):
return self.__name
def set_balance(self,new_b):
if isinstance(new_b,int):
self.__balance = new_b
else:
print("不能存放阴间的钱")
def get_balance(self):
return self.__balance
jack = Account('Jackma',99999999)
jack.show_info()
jack.set_balance(1000)
m = jack.get_balance()
print(m)
m -= 999
print(m)
二、继承
1.应用
class Phone(object):
def call(self,number):
print(f'正在给{number}打电话')
class iPhone(Phone):
def carmera(self):
print("拍照")
iphonex = iPhone()
iphonex.call('128447873929')
iphonex.carmera()
2.子类不能用父类的私有方法
class Father(object):
def __init__(self):
self.__money = 999
self.name = 'tom'
def __show(self):
print("这个是父类中的一个私有方法")
def display(self):
print("这个是父类中的一个私有方法")
class Son(Father):
def play(self):
print("这是子类中的方法")
#子类不能用父类的私有方法
self.__show()
s = Son()
s.play()
s.display()
3.子类初始化父类
class Father(object):
def __init__(self,name):
self.__money = 999
self.name = name
class Son(Father):
pass
#子类初始化父类的init
s = Son('tom')
print(s.name)
但子类有init方法会无法初始化父类
class Father(object):
def __init__(self,name):
self.__money = 999
self.name = name
class Son(Father):
def __init__(self,age):
self.age = age
#子类初始化父类的init
s = Son('tom')
print(s.name)
解决办法 就是手动init父类
4.子类重写和调用父类方法
class Father(object):
def cure(self):
print("父类")
class Son(Father):
def cure(self):
print("子类")
#子类初始化父类的init
s = Son()
s.cure()
class Father(object):
def cure(self):
print("父类")
class Son(Father):
def cure(self):
Father.cure(self)
print("子类")
#子类初始化父类的init
s = Son()
s.cure()
5.多层继承
class A(object):
def a(self):
print('a function')
class B(A):
def b(self):
print('b function')
class C(B):
def c(self):
print('c function')
class D(C):
def d(self):
print('d function')
d = D()
d.a()
d.b()
d.c()
d.d()
方法重写
class A(object):
def a(self):
print('a function')
class B(A):
def b(self):
print('b function')
class C(B):
#重写b
def b(self):
print('bc function')
def c(self):
print('c function')
class D(C):
def d(self):
print('d function')
d = D()
d.a()
d.b()
d.c()
d.d()
6.多层继承-初始化过程
class A(object):
def __init__(self,a):
self.a = a
class B(A):
def __init__(self,a,b):
A.__init__(self,a)
self.b = b
class C(B):
def __init__(self,a,b,c):
B.__init__(self, a,b)
self.c = c
class D(C):
def __init__(self,a,b,c,d):
C.__init__(self, a,b,c)
self.d = d
d = D(1,2,3,4)
print(d.a," ",d.b,' ',d.c," ",d.d)
7.多继承基本格式
class Father(object):
def func_fa(self):
print('Father Function')
class Mother(object):
def func_mo(self):
print('Mother function')
class Son(Father,Mother):
def play(self):
print('Son Play')
s = Son()
s.play()
s.func_mo()
s.func_fa()
8.多层多继承时的初始化问题
菱形继承
class Person(object):
def __init__(self,aaa):
print("Person")
self.aaa = aaa
class Father(Person):
def __init__(self,aaa,name):
Person.__init__(self,aaa)
print("Father")
self.name = name
class Mother(Person):
def __init__(self,aaa,age):
Person.__init__(self, aaa)
print("Mother")
self.age = age
class Son(Father,Mother):
def __init__(self,aaa,name,age,gender):
print("Son")
Mother.__init__(self,aaa,age)
Father.__init__(self,aaa,name)
self.gender = gender
s = Son(1,'tom',12,'男')
print("______________________________________________________")
print(s.aaa)
print(s.name)
print(s.age)
print(s.gender)
菱形继承的问题如何解决呢?
super解决
让子类调用father,father调用mather,mather调用person,变成线性的
class Person(object):
def __init__(self,aaa):
print("Person")
self.aaa = aaa
class Father(Person):
def __init__(self,aaa,name,age):
super(Father,self).__init__(aaa,age)
print("Father")
self.name = name
class Mother(Person):
def __init__(self,aaa,age):
super(Mother,self).__init__(aaa)
print("Mother")
self.age = age
class Son(Father,Mother):
def __init__(self,aaa,name,age,gender):
print("Son")
super(Son,self).__init__(aaa,name,age)
self.gender = gender
s = Son(1,'tom',12,'男')
print("______________________________________________________")
print(s.aaa)
print(s.name)
print(s.age)
print(s.gender)
super的执行过程
mro是个元组,元素的顺序是解释器定义的
注意根据这个列表我们可以得到son——father——mather——person这个顺序
如果顺序不对也会报错
class Person(object):
def __init__(self,aaa):
print("Person")
self.aaa = aaa
class Father(Person):
def __init__(self,aaa,name):
super(Father,self).__init__(aaa)
print("Father")
self.name = name
class Mother(Person):
def __init__(self,aaa,name,age):
super(Mother,self).__init__(aaa,name)
print("Mother")
self.age = age
class Son(Father,Mother):
def __init__(self,aaa,name,age,gender):
print("Son")
super(Son,self).__init__(aaa,name,age)
self.gender = gender
s = Son(1,'tom',12,'男')
print(Son.__mro__)
9.多继承初始化传参问题
class Person(object):
def __init__(self,aaa):
print("Person")
self.aaa = aaa
class Father(Person):
def __init__(self,name,*args):
#这里不用担心传参的问题,因为这里会自动解包
super(Father,self).__init__(*args)
print("Father")
self.name = name
class Mother(Person):
def __init__(self,age,aaa):
super(Mother,self).__init__(aaa)
print("Mother")
self.age = age
class Son(Father,Mother):
def __init__(self,gender,name,age,aaa):
print("Son")
super(Son,self).__init__(name,age,aaa)
self.gender = gender
s = Son('男','tom',12,1)
print(Son.__mro__)
print(s.name)
print(s.age)
print(s.gender)
print(s.aaa)
10.super简化写法
class Person(object):
def __init__(self,aaa):
print("Person")
self.aaa = aaa
class Father(Person):
def __init__(self,name,*args):
#这里不用担心传参的问题,因为这里会自动解包
#super(Father,self).__init__(*args)
super().__init__(*args)
print("Father")
self.name = name
class Mother(Person):
def __init__(self,age,aaa):
#super(Mother,self).__init__(aaa)
super().__init__(aaa)
print("Mother")
self.age = age
class Son(Father,Mother):
def __init__(self,gender,name,age,aaa):
print("Son")
#super(Son,self).__init__(name,age,aaa)
super().__init__(name, age, aaa)
self.gender = gender
s = Son('男','tom',12,1)
print(Son.__mro__)
print(s.name)
print(s.age)
print(s.gender)
print(s.aaa)
11.影响mro的顺序
类的额继承书写顺序会影响mro的顺序,但不会改变mro的顺序
'''
多重多继承时,方法的查找顺序也参考MRO
'''
class A(object):
pass
class B(A):
pass
class C(A):
pass
class D(B,C):
pass
print(D.__mro__)
'''
多重多继承时,方法的查找顺序也参考MRO
'''
class A(object):
pass
class B(A):
pass
class C(A):
pass
class D(C,B):
pass
print(D.__mro__)
最子类的参数书写顺序会影响mro的元素顺序
class A(object):
def show(self):
print('A show run ...')
class B(A):
def show(self):
print('B show run ...')
class C(A):
def show(self):
print('C show run ...')
class D(C,B):
pass
print(D.__mro__)
D().show()
class A(object):
def show(self):
print('A show run ...')
class B(A):
def show(self):
print('B show run ...')
class C(A):
def show(self):
print('C show run ...')
class D(B,C):
pass
print(D.__mro__)
D().show()
12.调用父类两种方法
super调用
class A(object):
def show(self):
print('A show run ...')
class B(A):
def show(self):
print('B show run ...')
class C(A):
def show(self):
super().show()
print('C show run ...')
class D(C,B):
pass
print(D.__mro__)
D().show()
直接调用
class A(object):
def show(self):
print('A show run ...')
class B(A):
def show(self):
print('B show run ...')
class C(A):
def show(self):
B().show()
print('C show run ...')
class D(C,B):
pass
print(D.__mro__)
D().show()
或者
class A(object):
def show(self):
print('A show run ...')
class B(A):
def show(self):
print('B show run ...')
class C(A):
def show(self):
B.show(self)
print('C show run ...')
class D(C,B):
pass
print(D.__mro__)
D().show()
因为B.show()的B不是一个对象实例
结语
上班时间不多,尽量多更新,点点赞吧!