本文摘自php中文网,作者巴扎黑,侵删。
封装1.为什么要封装?
封装就是要把数据属性和方法的具体实现细节隐藏起来,只提供一个接口。封装可以不用关心对象是如何构建的
2.封装包括数据的封装和函数的封装,数据的封装是为了保护隐私,函数的封装是为了隔离复杂度
3.数据的封装就是在属性前面加一个__
1 2 3 4 5 | 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'
|
咦,报错了,让我们打开对象的名称空间,看看发生了什么
1 | print (p.__dict__)#{ 'name' : 'zhang' , 'age' : 19, '_People__salary' : 100000}
|
哦,原来python把__salary变形成了_People__salary,再来一遍
1 | print (p._People__salary)#100000
|
所以,Python中并没有绝对的隐藏,只要你知道了上面这个,就无所谓隐藏了
这些变形操作,只在类的定义阶段或者对象定义(实例化阶段)阶段发生
虽然在外部无法直接访问加了__的属性,但是在类内部可以访问到,可以这么理解,在定义阶段,只要遇到__开头的,Python解释器自动识别为_类名__属性,所以在类内部是可以访问到的,这样的话,我们就可以搞一点小事情了
先来看这个
1 2 3 4 | 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
|
1 | #from B bar 别想多了,调用函数时别看定义位置,要看调用位置
|
如果就是想调用父类的bar()函数呢?该怎么做
1 2 3 4 | 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)不让外界看到我们的数据属性是怎么定义的,只能通过我们提供的接口,看到我们允许外界看到的内容
1 2 3 4 5 6 7 8 9 10 11 12 | 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)更常用的场景是,我们可以限制数据的类型,添加自己的逻辑以后再封装好给用户
1 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这个内置函数了
1 2 3 4 5 6 7 | 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
|
数据属性还应该有修改,删除操作
1 2 3 | @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面向对象的知识总结的详细内容,更多文章请关注木庄网络博客!!
相关阅读 >>
Python3.6.4如何安装到树莓派3代
Python中elif可以单独使用吗
Python如何实现单例模式
Python和selenium开发环境搭建的图文教程
Python实现requests发送/上传多个文件的示例
Python删除list中的重复元素
新浪新闻详情页的数据抓取实例
零基础学Python怎么打开图片
Python数据分析用什么数据库
Python中如何在一个函数中加入多个装饰器
更多相关阅读请进入《Python》频道 >>
人民邮电出版社
python入门书籍,非常畅销,超高好评,python官方公认好书。
转载请注明出处:木庄网络博客 » Python面向对象的知识总结