您好,欢迎访问一九零五行业门户网

Python面向对象的知识总结

封装
1.为什么要封装?
封装就是要把数据属性和方法的具体实现细节隐藏起来,只提供一个接口。封装可以不用关心对象是如何构建的
2.封装包括数据的封装和函数的封装,数据的封装是为了保护隐私,函数的封装是为了隔离复杂度
3.数据的封装就是在属性前面加一个__
class people:def __init__(self,name,age,salary):         self.name=name         self.age=age         self.__salary=salary p=people('zhang',19,100000)print(p.name)#zhangprint(p.age)#19print(p.__salary)#attributeerror: 'people' object has no attribute '__salary'
咦,报错了,让我们打开对象的名称空间,看看发生了什么
print(p.__dict__)#{'name': 'zhang', 'age': 19, '_people__salary': 100000}
哦,原来python把__salary变形成了_people__salary,再来一遍
print(p._people__salary)#100000
所以,python中并没有绝对的隐藏,只要你知道了上面这个,就无所谓隐藏了
这些变形操作,只在类的定义阶段或者对象定义(实例化阶段)阶段发生
虽然在外部无法直接访问加了__的属性,但是在类内部可以访问到,可以这么理解,在定义阶段,只要遇到__开头的,python解释器自动识别为_类名__属性,所以在类内部是可以访问到的,这样的话,我们就可以搞一点小事情了
先来看这个
class a:def foo(self):print('from a foo')         self.bar()def bar(self):print('from a bar')class b(a):def bar(self):print('from b bar') b=b() b.foo()  #from a foo
#from b bar  别想多了,调用函数时别看定义位置,要看调用位置
如果就是想调用父类的bar()函数呢?该怎么做
class a:def foo(self):print('from a foo')         self.__bar()def __bar(self):print('from a bar')class b(a):def __bar(self):print('from b bar') b=b() b.foo() #from a foo#from a bar有没有感受到编程的享受
4.封装的应用
1)不让外界看到我们的数据属性是怎么定义的,只能通过我们提供的接口,看到我们允许外界看到的内容
class people:def __init__(self,name,age,height,weight,hobby):         self.__name=name         self.__age=age         self.__height=height         self.__weight=weight         self._hobby=hobbydef tell_info(self):print('''name:%s         age:%s         height:%s         weeight:%s'''%(self.__name,self.__age,              self.__height,self.__weight)) p=people('zhang',18,1.90,75,'read') p.tell_info()
2)更常用的场景是,我们可以限制数据的类型,添加自己的逻辑以后再封装好给用户
    def tell_name(self):print(self.__name)#修改名字def set_name(self,new):if not isinstance(new,str):raise typeerror('名字必须是字符串类型')         self.__name=new
5.看我们上面的操作,用户查看名字的时候还得p.tell_name(),本来是个数据属性,却被我们搞得变成了一个函数,怎么伪装一下呢,就可以用到property这个内置函数了
class people:def __init__(self,name,age,height,weight,hobby):         self.__name=name         self.__age=age         self.__height=height         self.__weight=weight         self._hobby=hobby     @propertydef name(self):return self.__namep=people('zhang',18,1.90,75,'read')print(p.name)#zhang
数据属性还应该有修改,删除操作
    @propertydef name(self):return self.__name#name已经被property修饰过,就有setter和deleter    @name.setterdef name(self,new):if not isinstance(new,str):raise typeerror('名字必须是字符串类型')         self.__name=new     @name.deleterdef name(self):del self.__namep = people('zhang', 18, 1.90, 75, 'read')print(p.name)#zhangp.name='can'    #修改print(p.name)#candel p.name #删除print(p.name)#attributeerror: 'people' object has no attribute '_people__name'
以上就是python面向对象的知识总结的详细内容。
其它类似信息

推荐信息