Python--面向对象编程
Posted mr07lee
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python--面向对象编程相关的知识,希望对你有一定的参考价值。
面向对象编程--进阶
property属性
property是一种特殊的属性,访问它时会执行一段功能(函数)然后返回值
![技术分享图片](/img/jia.gif)
import math class Circle: def __init__(self,radius): #圆的半径radius self.radius=radius @property def area(self): return math.pi * self.radius**2 #计算面积 @property def perimeter(self): return 2*math.pi*self.radius #计算周长 c=Circle(10) print(c.radius) print(c.area) #可以向访问数据属性一样去访问area,会触发一个函数的执行,动态计算出一个值 print(c.perimeter) #同上 ‘‘‘ 输出结果: 314.1592653589793 62.83185307179586 ‘‘‘ #注意:此时的特性area和perimeter不能被赋值 c.area=3 #为特性area赋值 ‘‘‘ 抛出异常: AttributeError: can‘t set attribute ‘‘‘
为什么要用property
将一个类的函数定义成特性以后,对象再去使用的时候obj.name,根本无法察觉自己的name是执行了一个函数然后计算出来的,这种特性的使用方式遵循了统一访问的原则
ps:面向对象的封装有三种方式:
【public】
这种其实就是不封装,是对外公开的
【protected】
这种封装方式对外不公开,但对朋友(friend)或者子类(形象的说法是“儿子”,但我不知道为什么大家 不说“女儿”,就像“parent”本来是“父母”的意思,但中文都是叫“父类”)公开
【private】
这种封装对谁都不公开
python并没有在语法上把它们三个内建到自己的class机制中,在C++里一般会将所有的所有的数据都设置为私有的,然后提供set和get方法(接口)去设置和获取,在python中通过property方法可以实现
class Foo: def __init__(self,val): self.__NAME=val #将所有的数据属性都隐藏起来 @property def name(self): return self.__NAME #obj.name访问的是self.__NAME(这也是真实值的存放位置) @name.setter def name(self,value): if not isinstance(value,str): #在设定值之前进行类型检查 raise TypeError(‘%s must be str‘ %value) self.__NAME=value #通过类型检查后,将值value存放到真实的位置self.__NAME @name.deleter def name(self): raise TypeError(‘Can not delete‘) f=Foo(‘lee‘) print(f.name) # f.name=10 #抛出异常‘TypeError: 10 must be str‘ del f.name #抛出异常‘TypeError: Can not delete‘
一个静态属性property本质就是实现了get,set,delete三种方法
![技术分享图片](/img/jia.gif)
class Foo: @property def AAA(self): print(‘get的时候运行我啊‘) @AAA.setter def AAA(self,value): print(‘set的时候运行我啊‘) @AAA.deleter def AAA(self): print(‘delete的时候运行我啊‘) #只有在属性AAA定义property后才能定义AAA.setter,AAA.deleter f1=Foo() f1.AAA f1.AAA=‘aaa‘ del f1.AAA
![技术分享图片](/img/jia.gif)
class Foo: def get_AAA(self): print(‘get的时候运行我啊‘) def set_AAA(self,value): print(‘set的时候运行我啊‘) def delete_AAA(self): print(‘delete的时候运行我啊‘) AAA=property(get_AAA,set_AAA,delete_AAA) #内置property三个参数与get,set,delete一一对应 f1=Foo() f1.AAA f1.AAA=‘aaa‘ del f1.AAA
![技术分享图片](/img/jia.gif)
class Goods: def __init__(self): # 原价 self.original_price = 100 # 折扣 self.discount = 0.8 @property def price(self): # 实际价格 = 原价 * 折扣 new_price = self.original_price * self.discount return new_price @price.setter def price(self, value): self.original_price = value @price.deleter def price(self): del self.original_price obj = Goods() obj.price # 获取商品价格 obj.price = 200 # 修改商品原价 print(obj.price) del obj.price # 删除商品原价
classmethod
类方法
class Classmethod_Demo(): role = ‘dog‘ @classmethod def func(cls): print(cls.role) Classmethod_Demo.func()
staticmethod
静态方法
class Staticmethod_Demo(): role = ‘dog‘ @staticmethod def func(): print("当普通方法用") Staticmethod_Demo.func()
isinstance和issubclass
isinstance(obj,cls)检查是否obj是否是类 cls 的对象
class Foo(object): pass obj = Foo() isinstance(obj, Foo)
issubclass(sub, super)检查sub类是否是 super 类的派生类
class Foo(object): pass class Bar(Foo): pass issubclass(Bar, Foo)
反射
1 什么是反射
反射的概念是由Smith在1982年首次提出的,主要是指程序可以访问、检测和修改它本身状态或行为的一种能力(自省)。这一概念的提出很快引发了计算机科学领域关于应用反射性的研究。它首先被程序语言的设计领域所采用,并在Lisp和面向对象方面取得了成绩。
2 python面向对象中的反射:通过字符串的形式操作对象相关的属性。python中的一切事物都是对象(都可以使用反射)
四个可以实现自省的函数
下列方法适用于类和对象(一切皆对象,类本身也是一个对象)
![技术分享图片](/img/jia.gif)
def hasattr(*args, **kwargs): # real signature unknown """ Return whether the object has an attribute with the given name. This is done by calling getattr(obj, name) and catching AttributeError. """ pass
![技术分享图片](/img/jia.gif)
def getattr(object, name, default=None): # known special case of getattr """ getattr(object, name[, default]) -> value Get a named attribute from an object; getattr(x, ‘y‘) is equivalent to x.y. When a default argument is given, it is returned when the attribute doesn‘t exist; without it, an exception is raised in that case. """ pass
![技术分享图片](/img/jia.gif)
def setattr(x, y, v): # real signature unknown; restored from __doc__ """ Sets the named attribute on the given object to the specified value. setattr(x, ‘y‘, v) is equivalent to ``x.y = v‘‘ """ pass
![技术分享图片](/img/jia.gif)
def delattr(x, y): # real signature unknown; restored from __doc__ """ Deletes the named attribute from the given object. delattr(x, ‘y‘) is equivalent to ``del x.y‘‘ """ pass
![技术分享图片](/img/jia.gif)
class Foo: f = ‘类的静态变量‘ def __init__(self,name,age): self.name=name self.age=age def say_hi(self): print(‘hi,%s‘%self.name) obj=Foo(‘egon‘,73) #检测是否含有某属性 print(hasattr(obj,‘name‘)) print(hasattr(obj,‘say_hi‘)) #获取属性 n=getattr(obj,‘name‘) print(n) func=getattr(obj,‘say_hi‘) func() print(getattr(obj,‘aaaaaaaa‘,‘不存在啊‘)) #报错 #设置属性 setattr(obj,‘sb‘,True) setattr(obj,‘show_name‘,lambda self:self.name+‘sb‘) print(obj.__dict__) print(obj.show_name(obj)) #删除属性 delattr(obj,‘age‘) delattr(obj,‘show_name‘) delattr(obj,‘show_name111‘)#不存在,则报错 print(obj.__dict__)
![技术分享图片](/img/jia.gif)
class Foo(object): staticField = "old boy" def __init__(self): self.name = ‘wupeiqi‘ def func(self): return ‘func‘ @staticmethod def bar(): return ‘bar‘ print getattr(Foo, ‘staticField‘) print getattr(Foo, ‘func‘) print getattr(Foo, ‘bar‘)
![技术分享图片](/img/jia.gif)
import sys def s1(): print ‘s1‘ def s2(): print ‘s2‘ this_module = sys.modules[__name__] hasattr(this_module, ‘s1‘) getattr(this_module, ‘s2‘)
导入其他模块,利用反射查找该模块是否存在某个方法
![技术分享图片](/img/jia.gif)
def test(): print(‘from the test‘)
![技术分享图片](/img/jia.gif)
""" 程序目录: module_test.py index.py 当前文件: index.py """ import module_test as obj #obj.test() print(hasattr(obj,‘test‘)) getattr(obj,‘test‘)()
__str__和__repr__
改变对象的字符串显示__str__,__repr__
自定制格式化字符串__format__
![技术分享图片](/img/jia.gif)
format_dict={ ‘nat‘:‘{obj.name}-{obj.addr}-{obj.type}‘,#学校名-学校地址-学校类型 ‘tna‘:‘{obj.type}:{obj.name}:{obj.addr}‘,#学校类型:学校名:学校地址 ‘tan‘:‘{obj.type}/{obj.addr}/{obj.name}‘,#学校类型/学校地址/学校名 } class School: def __init__(self,name,addr,type): self.name=name self.addr=addr self.type=type def __repr__(self): return ‘School(%s,%s)‘ %(self.name,self.addr) def __str__(self): return ‘(%s,%s)‘ %(self.name,self.addr) def __format__(self, format_spec): # if format_spec if not format_spec or format_spec not in format_dict: format_spec=‘nat‘ fmt=format_dict[format_spec] return fmt.format(obj=self) s1=School(‘oldboy1‘,‘北京‘,‘私立‘) print(‘from repr: ‘,repr(s1)) print(‘from str: ‘,str(s1)) print(s1) ‘‘‘ str函数或者print函数--->obj.__str__() repr或者交互式解释器--->obj.__repr__() 如果__str__没有被定义,那么就会使用__repr__来代替输出 注意:这俩方法的返回值必须是字符串,否则抛出异常 ‘‘‘ print(format(s1,‘nat‘)) print(format(s1,‘tna‘)) print(format(s1,‘tan‘)) print(format(s1,‘asfdasdffd‘))
![技术分享图片](/img/jia.gif)
class B: def __str__(self): return ‘str : class B‘ def __repr__(self): return ‘repr : class B‘ b=B() print(‘%s‘%b) print(‘%r‘%b)
__del__
析构方法,当对象在内存中被释放时,自动触发执行。
注:此方法一般无须定义,因为Python是一门高级语言,程序员在使用时无需关心内存的分配和释放,因为此工作都是交给Python解释器来执行,所以,析构函数的调用是由解释器在进行垃圾回收时自动触发执行的。
![技术分享图片](/img/jia.gif)
class Foo: def __del__(self): print(‘执行我啦‘) f1=Foo() del f1 print(‘------->‘) #输出结果 执行我啦 ------->
item系列
__getitem__/setitem__/__delitem__
![技术分享图片](/img/jia.gif)
class Foo: def __init__(self,name): self.name=name def __getitem__(self, item): print(self.__dict__[item]) def __setitem__(self, key, value): self.__dict__[key]=value def __delitem__(self, key): print(‘del obj[key]时,我执行‘) self.__dict__.pop(key) def __delattr__(self, item): print(‘del obj.key时,我执行‘) self.__dict__.pop(item) f1=Foo(‘lyq‘) f1[‘age‘]=18 f1[‘age1‘]=19 del f1.age1 del f1[‘age‘] f1[‘name‘]=‘lee‘ print(f1.__dict__)
__new__
![技术分享图片](/img/jia.gif)
class A: def __init__(self): self.x = 1 print(‘in init function‘) def __new__(cls, *args, **kwargs): print(‘in new function‘) return object.__new__(A, *args, **kwargs) a = A() print(a.x)
![技术分享图片](/img/jia.gif)
class Singleton: def __new__(cls, *args, **kw): if not hasattr(cls, ‘_instance‘): cls._instance = object.__new__(cls, *args, **kw) return cls._instance one = Singleton() two = Singleton() two.a = 3 print(one.a) # 3 # one和two完全相同,可以用id(), ==, is检测 print(id(one)) # 29097904 print(id(two)) # 29097904 print(one == two) # True print(one is two)
__call__
对象后面加括号,触发执行。
注:构造方法的执行是由创建对象触发的,即:对象 = 类名() ;而对于 __call__ 方法的执行是由对象后加括号触发的,即:对象() 或者 类()()
![技术分享图片](/img/jia.gif)
class Foo: def __init__(self): pass def __call__(self, *args, **kwargs): print(‘__call__‘) obj = Foo() # 执行 __init__ obj() # 执行 __call__
__len__
![技术分享图片](/img/jia.gif)
class A: def __init__(self): self.a = 1 self.b = 2 def __len__(self): return len(self.__dict__) a = A() print(len(a))
__hash__
![技术分享图片](/img/jia.gif)
class A: def __init__(self): self.a = 1 self.b = 2 def __hash__(self): return hash(str(self.a)+str(self.b)) a = A() print(hash(a))
__eq__
![技术分享图片](/img/jia.gif)
class A: def __init__(self): self.a = 1 self.b = 2 def __eq__(self,obj): if self.a == obj.a and self.b == obj.b: return True a = A() b = A() print(a == b)
![技术分享图片](/img/jia.gif)
class Person: def __init__(self,name,age,sex): self.name = name self.age = age self.sex = sex def __hash__(self): return hash(self.name+self.sex) def __eq__(self, other): if self.name == other.name and self.sex == other.sex:return True p_lst = [] for i in range(84): p_lst.append(Person(‘lee‘,i,‘male‘)) print(p_lst) print(set(p_lst))
面向对象常用术语
抽象/实现
抽象指对现实世界问题和实体的本质表现,行为和特征建模,建立一个相关的子集,可以用于 绘程序结构,从而实现这种模型。抽象不仅包括这种模型的数据属性,还定义了这些数据的接口。
对某种抽象的实现就是对此数据及与之相关接口的现实化(realization)。现实化这个过程对于客户 程序应当是透明而且无关的。
封装/接口
封装描述了对数据/信息进行隐藏的观念,它对数据属性提供接口和访问函数。通过任何客户端直接对数据的访问,无视接口,与封装性都是背道而驰的,除非程序员允许这些操作。作为实现的 一部分,客户端根本就不需要知道在封装之后,数据属性是如何组织的。在Python中,所有的类属性都是公开的,但名字可能被“混淆”了,以阻止未经授权的访问,但仅此而已,再没有其他预防措施了。这就需要在设计时,对数据提供相应的接口,以免客户程序通过不规范的操作来存取封装的数据属性。
注意:封装绝不是等于“把不想让别人看到、以后可能修改的东西用private隐藏起来”
真正的封装是,经过深入的思考,做出良好的抽象,给出“完整且最小”的接口,并使得内部细节可以对外透明
(注意:对外透明的意思是,外部调用者可以顺利的得到自己想要的任何功能,完全意识不到内部细节的存在)
合成
合成扩充了对类的 述,使得多个不同的类合成为一个大的类,来解决现实问题。合成 述了 一个异常复杂的系统,比如一个类由其它类组成,更小的组件也可能是其它的类,数据属性及行为, 所有这些合在一起,彼此是“有一个”的关系。
派生/继承/继承结构
派生描述了子类衍生出新的特性,新类保留已存类类型中所有需要的数据和行为,但允许修改或者其它的自定义操作,都不会修改原类的定义。
继承描述了子类属性从祖先类继承这样一种方式
继承结构表示多“代”派生,可以述成一个“族谱”,连续的子类,与祖先类都有关系。
泛化/特化
基于继承
泛化表示所有子类与其父类及祖先类有一样的特点。
特化描述所有子类的自定义,也就是,什么属性让它与其祖先类不同。
多态与多态性
多态指的是同一种事物的多种状态:水这种事物有多种不同的状态:冰,水蒸气
多态性的概念指出了对象如何通过他们共同的属性和动作来操作及访问,而不需考虑他们具体的类。
冰,水蒸气,都继承于水,它们都有一个同名的方法就是变成云,但是冰.变云(),与水蒸气.变云()是截然不同的过程,虽然调用的方法都一样
自省/反射
自省也称作反射,这个性质展示了某对象是如何在运行期取得自身信息的。如果传一个对象给你,你可以查出它有什么能力,这是一项强大的特性。如果Python不支持某种形式的自省功能,dir和type内建函数,将很难正常工作。还有那些特殊属性,像__dict__,__name__及__doc__
以上是关于Python--面向对象编程的主要内容,如果未能解决你的问题,请参考以下文章