1、運算概念的理解
運算(Operation
)是操作邏輯的抽象
- 運算體現一種操作邏輯,在廣義角度來說任何程序都是一種運算
-
Python
解釋器通過保留方法預留了一批運算的接口,需要重載 -
保留方法一般對應運算符,
Python
中運算體現為運算符的重載 - 運算本質上體現了交互關系、包含關系和常規的操作關系
運算重載的限制
-
不能重載
Python
語言內置類型的運算符 - 不能新建運算符,只能通過重載完成
-
is
,and
,not
,or
不能被重載
2、運算符的重載
2.1 算術運算符
一元運算符:+
、-
、~
二元運算符:+
、-
、*
、/
、//
、%
、divmod()
、 pow()
、**
、<<
、>>
、&
、^
、|
保留方法 | 對應操作 | 描述 |
---|---|---|
.__neg__(self) | -obj | 定義對象取負的運算邏輯 |
.__pos__(self) | +obj | 定義對象取正的運算邏輯 |
.__abs__(self) | abs(obj) | 定義對象絕對值的運算邏輯 |
.__invert__(self) | ~obj | 定義對象取反的運算邏輯 |
.__add__(self, other) | obj + other | 定義了兩個對象加法的運算邏輯 |
.__sub__(self, other) | obj - other | 定義了兩個對象減法的運算邏輯 |
.__mul__(self, other) | obj * other | 定義了兩個對象乘法的運算邏輯 |
.__truediv__(self, other) | obj / other | 定義了兩個對象除法的運算邏輯 |
.__floordiv__(self, other) | obj // other | 定義了兩個對象整數除的運算邏輯 |
.__mod__(self, other) | obj % other | 定義了兩個對象模的運算邏輯 |
.__divmod__(self, other) | divmod(obj, other) | 定義了兩個對象除模的運算邏輯 |
.__pow__(self, other) | obj ** other | 定義對象冪的運算邏輯 |
.__lshift__(self, other) | obj << other | 定義對象左移的運算邏輯 |
.__rshift__(self, other) | obj >> other | 定義對象右移的運算邏輯 |
.__and__(self, other) | obj & other | 定義兩個對象位于運算邏輯 |
.__xor__(self, other) | obj ^ other | 定義兩個對象位異或的運算邏輯 |
.__or__(self, other) | `obj | other` |
2.2 比較運算符
比較運算符:<、<=、==、!=、>、>=
保留方法 | 對應操作 |
---|---|
.__lt__(self, other) | obj < other |
.__le__(self, other) | obj <= other |
.__eq__(self, other) | obj == other |
.__be__(self, other) | obj != other |
.__gt__(self, other) | obj > other |
.__ge__(self, other) | obj >= other |
兩個對象比較操作的運算重載
2.3 成員運算
成員獲取:[]
、def
、 .eversed()
成員判斷:in
保留方法 | 對應操作 | 描述 |
---|---|---|
.__getitem__(self, key) | obj[k] | 定義獲取對象中序號K元素的運算邏輯,K為整數 |
.__setitem__(self, key, v) | obj[k] = v | 定義賦值對象中序號K元素的運算邏輯 |
.__delitem__(self, key) | del obj[k] | 定義刪除對象中序號K元素的運算邏輯 |
.__reversed__(self) | obj.reversed() | 定義對象逆序的運算邏輯 |
.__contains__(self, item) | item in obj | 定義in操作符對應的運算邏輯 |
2.4 其他運算
Python
內置函數:rep(),str(),len(),int(),flaot,complex(),round(),bytes(),bool(),format(),.format
(常用方法)
保留方法 | 對應操作 | 描述 |
---|---|---|
__repr__(self) | repr(obj) | 定義對象可打印字符串的運算邏輯 |
__str__(self) | str(obj) | 定義對象字符串轉換的運算邏輯 |
__len__(self) | len(obj) | 定義對象長度操作的運算邏輯 |
__int__(self) | int(obj) | 定義對象整數轉換的運算邏輯 |
__float__(self) | float(obj) | 定義對象浮點數轉換的運算邏輯 |
__complex__(self) | complex(obj) | 定義對象復數轉換的運算邏輯 |
__round__(self) | round(obj) | 定義對象四舍五入的運算邏輯 |
__bytes__(self) | bytes(obj) | 定義對象字節串轉換的運算邏輯 |
__bool__(self) | bool(obj) | 定義對象布爾運算的運算邏輯 |
.__format__(self, format_spec) | obj.format() format(obj) | 定義對象格式化輸出的運算邏輯 |
3、Python類的多態
多態 _(Polymorphism
)_是針對方法,體現方法靈活性的多態;簡單的說,他包含兩部分
參數類型的多態:一個方法能夠處理多個類型的能力
Python
的函數/方法沒有類型聲明限制,天然支持參數類型的多態性
Python
編程理念在于:文檔約束,而非語法約束
對不同參數類型的區分以及功能,需要有程序員完成
參數形式的多態:一個方法能夠接受多個參數的能力
Python
的函數/方法可以支持可變參數,支持參數形式的多態性
Python
的類方法也是函數,函數的各種定義方式均有效
對不同參數個數以及默認值的確定,需要由程序員完成
多態是OOP的一個傳統概念,Python天然支持多態,不需要特殊語法,示例代碼如下所示:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
import abc class Animal(metaclass = abc.ABCMeta): #同一類事物:動物 @abc .abstractmethod def talk( self ): pass class Cat(Animal): #動物的形態之一:貓 def talk( self ): print ( 'say miaomiao' ) class Dog(Animal): #動物的形態之二:狗 def talk( self ): print ( 'say wangwang' ) class Pig(Animal): #動物的形態之三:豬 def talk( self ): print ( 'say aoao' ) |
到此這篇關于Python
面向對象編程之類的運算的文章就介紹到這了,更多相關Python類的運算內容請搜索服務器之家以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持服務器之家!
原文鏈接:https://juejin.cn/post/7023557519188426782