一、私有和公有属性的定义和使用
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()