当前位置: 移动技术网 > IT编程>脚本编程>Python > day26-python之封装

day26-python之封装

2019年08月17日  | 移动技术网IT编程  | 我要评论

什么是白喉,磁流变阻尼器,360wifi连接上但上不了网

1.动态导入模块

# module_t=__import__('m1.t')
# print(module_t)

# module_t = __import__('m1.t')
# print(module_t)


# module_t.t.test1()
# from m1.t import *
# from m1.t import test1,_test2
#
# test1()
# _test2()

# module_t.t.test1()
# from m1.t import *
# from m1.t import  test1,_test2
# test1()
# _test2()


# import  importlib
# m=importlib.import_module('m1.t')
# print(m)
# m.test1()
# m._test2()


import  importlib
m = importlib.import_module('m1.t')
print(m)
m.test1()
m._test2()

2.包装标准类型

# class list(list):
#     def append(self, p_object):
#         if type(p_object) is str:
#             # self.append(p_object)
#             super().append(p_object)
#         else:
#             print('只能添加字符串类型')
#
#     def show_midlle(self):
#         mid_index=int(len(self)/2)
#         return self[mid_index]
class list(list):
    def  append(self, p_object):
        if type(p_object) is str:
            super().append(p_object)
        else:
            print('只能添加字符串类型')

    def show_midlle(self):
        mid_index = int(len(self)/2)
        return self[mid_index]

l1 = list('helloworld')
print(l1,type(l1))
print(l1.show_midlle())
# l1.append(111111111111111)
l1.append('sb')
print(l1)

# l2=list('hell oworld')
# print(l2,type(l2))

# l1=list('helloworld')
# print(l1,type(l1))
# print(l1.show_midlle())
# l1.append(1111111111111111111111)
# l1.append('sb')
# print(l1)

3.双下划线开头的attr方法:

# class foo:
#     x=1
#     def __init__(self,y):
#         self.y=y
#
#     def __getattr__(self, item):
#         print('执行__getattr__')
#
# f1=foo(10)
# print(f1.y)
# print(getattr(f1,'y'))   #len(str)--->str.__len__()
# f1.sssssssssssssssssssssssssssssssssssss




# class foo:
#     x=1
#     def __init__(self,y):
#         self.y=y
#
#     def __delattr__(self, item):
#         print('删除操作__delattr__')
#
# f1=foo(10)
# del f1.y
# del f1.x



#
# class foo:
#     x=1
#     def __init__(self,y):
#         self.y=y
#
#     def __setattr__(self, key, value):
#         print('__setattr__执行')
#         # self.key=value
#         self.__dict__[key]=value
# f1=foo(10)
# print(f1.__dict__)
# f1.z=2
# print(f1.__dict__)








# class foo:
#     def __getattr__(self, item):
#         print('------------->')
#
# # print(foo.__dict__)
# print(dir(foo))
# f1=foo()
#
# print(f1.x)  #只有在属性不存在时,会自动触发__getattr__
#
# del f1.x #删除属性时会触发_delattr__
#
# f1.y=10
# f1.x=3  # 设置属性的时候会触发——setattr———











# class foo:
#     def __init__(self,name):
#         self.name=name
#     def __getattr__(self, item):
#         print('你找的属性【%s】不存在' %item)
#     def __setattr__(self, k,v):
#         print('执行setattr',k,v)
#         if type(v) is str:
#             print('开始设置')
#             # self.k=v #触发__setattr__
#             self.__dict__[k]=v.upper()
#         else:
#             print('必须是字符串类型')
#     def __delattr__(self, item):
#         print('不允许删除属性【%s】' %item)
        # print('执行delattr',item)
        # del self.item
        # self.__dict__.pop(item)
 




# f1=foo('alex')
# f1.age=18 #触发__setattr__
# print(f1.__dict__)
# print(f1.name)
# print(f1.age)
# print(f1.gender)
# print(f1.slary)
# print(f1.__dict__)
# del f1.name
# print(f1.__dict__)

4.反射

# class blackmedium:
#     feture='ugly'
#     def __init__(self,name,addr):
#         self.name=name
#         self.addr=addr
#
#     def sell_hourse(self):
#         print('【%s】 正在卖房子,傻逼才买呢' %self.name)
#
#     def rent_hourse(self):
#         print('【%s】 正在租房子,傻逼才租呢' % self.name)
#
#
# print(hasattr(blackmedium,'feture'))
# getattr()

class blackmedium:
    feture='ugly'
    def __init__(self,name,addr):
        self.name = name
        self.addr = addr

    def sell_hourse(self):
        print('[%s]正在卖房子,傻逼才买呢'%self.name)

    def rent_hourse(self):
        print('[%s]正在租房子,傻逼才租呢'%self.name)

b1 = blackmedium('万成置地', '天露园')



#
# b1=blackmedium('万成置地','天露园')
# b1.name--->b1.__dic__['name']
# print(b1.__dict__)
#
# # b1.name
# # b1.sell_hourse
# print(hasattr(b1,'name'))
# print(hasattr(b1,'sell_hourse'))
# print(hasattr(b1,'selasdfasdfsadfasdfasdfasdfasdl_hourse'))
#
#
#
# print(getattr(b1,'name'))
# print(getattr(b1,'rent_hourse'))
# func=getattr(b1,'rent_hourse')
# func()
# # print(getattr(b1,'rent_hourseasdfsa')) #没有则报错
# print(getattr(b1,'rent_hourseasdfsa','没有这个属性')) #没有则报错

b1.sb = true
setattr(b1,'sb',true)
setattr(b1,'sb1',123)
setattr(b1,'name','sb')
setattr(b1,'func',lambda x:x+1)
setattr(b1,'func1',lambda self:self.name+'sb')

print(b1.func1(b1))
#
#
# # b1.sb=true
# setattr(b1,'sb',true)
# setattr(b1,'sb1',123)
# setattr(b1,'name','sb')
# setattr(b1,'func',lambda x:x+1)
# setattr(b1,'func1',lambda self:self.name+'sb')
# print(b1.__dict__)
# print(b1.func)
# print(b1.func(10))
# print(b1.func1(b1))
# del b1.sb
# del b1.sb1
# delattr(b1,'sb')
# print(b1.__dict__)

 

5.多态

#_*_coding:utf-8_*_
# __author__ = 'linhaifeng'
# class h2o:
#     def __init__(self,name,temperature):
#         self.name=name
#         self.temperature=temperature
#     def turn_ice(self):
#         if self.temperature < 0:
#             print('[%s]温度太低结冰了' %self.name)
#         elif self.temperature > 0 and self.temperature < 100:
#             print('[%s]液化成水' %self.name)
#         elif self.temperature > 100:
#             print('[%s]温度太高变成了水蒸气' %self.name)
#     def aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(self):
#         pass
__author__ = 'shiqianyu'
class h2o:
    def __init__(self,name,temperature):
        self.name = name
        self.temperature = temperature
    def turn_ice(self):
        if self.temperature<0:
            print('[%s]温度太低结冰了'%self.name)
        elif self.temperature>0 and self.temperature<100:
            print('[%s]液化成水'%self.name)
        elif self.temperature>100:
            print('[%s]温度太高变成了水蒸汽'%self.name)
    def aaaaaaaaaaaaaaa(self):
        pass
class water(h2o):
    pass
class ice(h2o):
    pass
class steam(h2o):
    pass


# class water(h2o):
#     pass
# class ice(h2o):
#     pass
# class steam(h2o):
#     pass

w1=water('水',25)
i1=ice('冰',-20)
s1=steam('蒸汽',3000)

# w1.turn_ice()
# i1.turn_ice()
# s1.turn_ice()

# def func(obj):
#     obj.turn_ice()

# func(w1)  #---->w1.turn_ice()
# func(i1)  #---->i1.turn_ice()
# def func(obj):
#     obj.turn_ice()
#
# func(w1)
# func(i1)
# func(s1)


def func(obj):
    obj.turn_ice()

func(w1)
func(i1)
func(s1)

6.封装示范一

#_*_coding:utf-8_*_
# __author__ = 'linhaifeng'
#
# class room:
#     def __init__(self,name,owner,width,length,high):
#         self.name=name
#         self.owner=owner
#         self.__width=width
#         self.__length=length
#         self.__high=high
#
#     def tell_area(self): #此时我们想求的是面积
#         return self.__width * self.__length *self.__high
#
#     def tell_width(self):
#         return self.__width

__author__ = 'shiqianyu'
class room:
    def __init__(self,name,owner,width,length,high):
        self.name = name
        self.owner = owner
        self.__width = width
        self.__length = length
        self.__high = high

    def tell_area(self):
        return self.__width*self.__length*self.__high

    def tell_width(self):
        return self.__width

r1 = room('卫生','alex',100,100,500)
# area = r1.__width*r1.__length
print(r1.tell_area())

# r1=room('卫生间','alex',100,100,10000)

# arear=r1.__width * r1.__length
# print(r1.tell_area())

 7.授权

# import time
# class filehandle:
#     def __init__(self,filename,mode='r',encoding='utf-8'):
#         # self.filename=filename
#         self.file=open(filename,mode,encoding=encoding)
#         self.mode=mode
#         self.encoding=encoding
#     def write(self,line):
#         print('------------>',line)
#         t=time.strftime('%y-%m-%d %x')
#         self.file.write('%s %s' %(t,line))
#
#     def __getattr__(self, item):
#         # print(item,type(item))
#         # self.file.read
#         return getattr(self.file,item)
import  time
class filehandle:
    def __init__(self,filename,mode='r',encoding='utf-8'):
        self.file = open(filename,mode,encoding=encoding)
        self.mode = mode
        self.encoding = encoding

    def write(self,line):
        print('-------------------->',line)
        t = time.strftime('%y-%m-%d %x')
        self.file.write('%s %s'%(t,line))

    def __getattr__(self, item):
        return getattr(self.file,item)

f1 = filehandle('a.txt','w+')
f1.write('111111111111111\n')
f1.write('cpu负载过高\n')
f1.write('内存剩余不足\n')
f1.write('硬盘剩余不足\n')
f1.seek(0)
print(f1.read())




# f1=filehandle('a.txt','w+')
# print(f1.file)
# print(f1.__dict__)
# print('==>',f1.read) #触发__getattr__
# print(f1.write)
# f1.write('1111111111111111\n')
# f1.write('cpu负载过高\n')
# f1.write('内存剩余不足\n')
# f1.write('硬盘剩余不足\n')
# f1.seek(0)
# print('--->',f1.read())

 

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网