本篇文章仍然是面向对象的相关内容,主要分析了封装、反射、动态导入模块、类内置attr属性、包装与授权三大面向对象编程的后续处理方法
封装
封装的本质:
把方法放到类里、把类放到模块里就叫装;
而其他地方导入模块、引用类、调用方法时看不到对应的模块、类、方法的具体逻辑,只知道名字就可以,因为它们是隐藏着的,这就叫封
封装的本质就要明确区分内外,因此封装是一种思想,内部就是内部,外部就是外部
class People:
_place= 'Beijing'
__position= 'Python'
def __init__(self,id_num,name,age,salary):
self.id_num= id_num
self.name= name
self.age= age
self.salary= salary
def get_id(self):
print('I am a privacy,我需要找的id为【%s】' %self.id_num)
v1= People('12345678','Zoro',22,2000000)
print(v1._place)
print(v1._People__position
运行结果:
![封装](https://img-blog.csdnimg.cn/20200316185547660.png)
真正效果上的封装: 明确区分内外,内部的实现逻辑,外部无法知道,并且为封装到内部的逻辑提供一个访问接口给外部使用
反射
反射的本质:
也叫做程序的自省,主要指程序可以访问、检测和修改它本身状态的一种能力
class BadGuy:
feature= 'Ugly'
def __init__(self,name,address):
self.name = name
self.address= address
def sell_house(self):
print('【%s】 正在卖房子'%self.name)
def rent_house(self):
print('【%s】 正在租房子' %self.name)
p1= BadGuy('黑中介','沙河')
print(hasattr(p1,'name'))
print(hasattr(p1,'sell_house'))
print(getattr(p1,'name'))
print(getattr(p1,'rent_house'))
print(getattr(p1,'r_house','该属性不存在'))
setattr(p1,'新世界第五皇','路飞')
print(p1.__dict__)
setattr(p1,'add_num',lambda x:x+1)
setattr(p1,'join_str',lambda self:self.name+'是BadGuy')
print(p1.add_num(1313))
print(p1.join_str(p1))
delattr(p1,'新世界第五皇')
print(p1.__dict__)
运行结果:
![反射](https://img-blog.csdnimg.cn/20200316185748176.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L1ZpZXdpbmZpbml0ZWx5,size_16,color_FFFFFF,t_70)
动态导入模块
首先自定义一个简单的模块,放到web文件夹组成一个包:
![自定义模块](https://img-blog.csdnimg.cn/20200316190050849.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L1ZpZXdpbmZpbml0ZWx5,size_16,color_FFFFFF,t_70)
module = __import__('web.test')
print(module)
module.test.test1()
import importlib
m = importlib.import_module('web.test')
print(m)
m.test2()
运行结果:
![动态导入模块](https://img-blog.csdnimg.cn/20200316185936181.png)
类内置attr属性
总共有三大类内置attr属性:
getattr
setattr
delattr
class Test:
y = 2020
def __init__(self,x):
self.x= x
def __getattr__(self,item):
print('正在执行__getattr__方法')
def __delattr__(self,item):
print('正在触发__delattr__操作')
def __setattr__(self,key,value):
print('正在用__setattr__设置值')
self.__dict__[key]= value
p1= Test(520)
print(p1.x)
print(getattr(p1,'__getattr__'))
p1.jjjjjjjjjjjjjjjjjj
del p1.y
print(p1.__dict__)
p1.y= 13
p1.z= 14
print(p1.__dict__)
运行结果:
![类内置attr属性](https://img-blog.csdnimg.cn/20200316190328412.png)
__getattr__的好处:
当传入不存在的属性时不会报错,而是输出对应的提醒
class Test:
y = 2020
def __init__(self,x):
self.x= x
def __getattr__(self,item):
print('你找的属性【%s】不存在!' %item)
p1= Test(520)
print(p1.x)
print(getattr(p1,'__getattr__'))
p1.jjjjjjjjjjjjjjjjjj
运行结果:
![__getattr__的好处](https://img-blog.csdnimg.cn/20200316190441982.png)
包装与授权
包装: 包装一个类型通常是对已经存在的类型进行一些定制,可以新建、修改、删除原有产品的功能,其他的保持原样
授权: 授权是包装的一个特性,所有更新的功能都是由新类的某部分来处理,但已存在的功能就授权给对象的默认属性;实现授权的关键点就是覆盖__getattr__方法
import time
class Open:
def __init__(self,filename,mode,encoding):
self.file= open(filename,mode,encoding=encoding)
self.mode= mode
self.encoding= encoding
def __getattr__(self,item):
print('【%s】不存在!' %item)
print(item,type(item))
return getattr(self.file, item)
def write(self,line):
t = time.strftime('%Y-%m-%d %t')
self.file.write(t + line)
p1= Open('a.txt','r+','gbk')
print(p1.file)
p1.read
print(p1.write)
p1.write('Is ok...\n')
p1.write('终于可以写进来了\n')
p1.file.close()
运行结果:
![包装与授权](https://img-blog.csdnimg.cn/20200316190712314.png)
在文件中也正常写入了内容:
![授权](https://img-blog.csdnimg.cn/20200316190756593.png)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)