Python最近挺火呀,比鹿晗薛之谦还要火,当然是在程序员之间。下面我们看看有关Python的相关内容。
上一篇文章我们已经介绍了部分Python面向对象编程基础的知识,大家可以参阅:Python面向对象编程基础解析(一),接下来,我们看看另一篇。
封装
1.为什么要封装?
封装就是要把数据属性和方法的具体实现细节隐藏起来,只提供一个接口。封装可以不用关心对象是如何构建的,其实在面向对象中,封装其实是最考验水平的
2.封装包括数据的封装和函数的封装,数据的封装是为了保护隐私,函数的封装是为了隔离复杂度
3.数据的封装就是在属性前面加一个__
class People: def __init__(self,name,age,salary): self.name=name self.age=age self.__salary=salaryp=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=hobby def 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()
新闻热点
疑难解答