# Python 類
和其它編程語言相比,Python 在盡可能不增加新的語法和語義的情況下加入了類機制。
Python中的類提供了面向對象編程的所有基本功能:類的繼承機制允許多個基類,派生類可以覆蓋基類中的任何方法,方法中可以調用基類中的同名方法。
對象可以包含任意數量和類型的數據。
## 類定義
語法格式如下:
```
class ClassName:
<statement-1>
.
.
.
<statement-N>
```
類實例化后,可以使用其屬性,實際上,創建一個類之后,可以通過類名訪問其屬性。
## 類對象
類對象支持兩種操作:屬性引用和實例化。
屬性引用使用和 Python 中所有的屬性引用一樣的標準語法:obj.name。
類對象創建后,類命名空間中所有的命名都是有效屬性名。所以如果類定義是這樣:
```
class MyClass:
"""A simple example class"""
i = 12345
def f(self):
return 'hello world'
```
實例化類:
```
x = MyClass()
```
以上創建了一個新的類實例并將該對象賦給局部變量 x,x 為空的對象。
很多類都傾向于將對象創建為有初始狀態的。因此類可能會定義一個名為 __init__() 的特殊方法(構造方法),像下面這樣:
```
def __init__(self):
self.data = []
```
類定義了 __init__() 方法的話,類的實例化操作會自動調用 __init__() 方法。所以在下例中,可以這樣創建一個新的實例:
```
x = MyClass()
```
當然, __init__() 方法可以有參數,參數通過 __init__() 傳遞到類的實例化操作上。例如:
```
>>> class Complex:
... def __init__(self, realpart, imagpart):
... self.r = realpart
... self.i = imagpart
...
>>> x = Complex(3.0, -4.5)
>>> x.r, x.i
(3.0, -4.5)
```
## 類的方法
在類地內部,使用def關鍵字可以為類定義一個方法,與一般函數定義不同,類方法必須包含參數self,且為第一個參數:
```
#類定義
class people:
#定義基本屬性
name = ''
age = 0
#定義私有屬性,私有屬性在類外部無法直接進行訪問
__weight = 0
#定義構造方法
def __init__(self,n,a,w):
self.name = n
self.age = a
self.__weight = w
def speak(self):
print("%s is speaking: I am %d years old" %(self.name,self.age))
p = people('tom',10,30)
p.speak()
```
## 繼承
Python 同樣支持類的繼承,如果一種語言不支持繼承就,類就沒有什么意義。派生類的定義如下所示:
```
class DerivedClassName(BaseClassName1):
<statement-1>
.
.
.
<statement-N>
```
需要注意圓括號中基類的順序,若是基類中有相同的方法名,而在子類使用時未指定,python從左至右搜索 即方法在子類中未找到時,從左到右查找基類中是否包含方法。
BaseClassName(示例中的基類名)必須與派生類定義在一個作用域內。除了類,還可以用表達式,基類定義在另一個模塊中時這一點非常有用:
```
class DerivedClassName(modname.BaseClassName):
```
### 實例
```
#單繼承示例
class student(people):
grade = ''
def __init__(self,n,a,w,g):
#調用父類的構函
people.__init__(self,n,a,w)
self.grade = g
#覆寫父類的方法
def speak(self):
print("%s is speaking: I am %d years old,and I am in grade %d"%(self.name,self.age,self.grade))
s = student('ken',20,60,3)
s.speak()
```
## 多重繼承
Python同樣有限的支持多繼承形式。多繼承的類定義形如下例:
```
class DerivedClassName(Base1, Base2, Base3):
<statement-1>
.
.
.
<statement-N>
```
需要注意圓括號中父類的順序,若是父類中有相同的方法名,而在子類使用時未指定,python從左至右搜索 即方法在子類中未找到時,從左到右查找父類中是否包含方法。
```
#另一個類,多重繼承之前的準備
class speaker():
topic = ''
name = ''
def __init__(self,n,t):
self.name = n
self.topic = t
def speak(self):
print("I am %s,I am a speaker!My topic is %s"%(self.name,self.topic))
#多重繼承
class sample(speaker,student):
a =''
def __init__(self,n,a,w,g,t):
student.__init__(self,n,a,w,g)
speaker.__init__(self,n,t)
test = sample("Tim",25,80,4,"Python")
test.speak()#方法名同,默認調用的是在括號中排前地父類的方法
```
## 類私有方法
__private_method 兩個下劃線開頭,聲明該方法為私有方法,不能在類地外部調用。
在類的內部調用slef.__private_methods。
類的專有方法:
* __init__ 構造函數,在生成對象時調用
* __del__ 析構函數,釋放對象時使用
* __repr__ 打印,轉換
* __setitem__按照索引賦值
* __getitem__按照索引獲取值
* __len__獲得長度
* __cmp__比較運算
* __call__函數調用
* __add__加運算
* __sub__減運算
* __mul__乘運算
* __div__除運算
* __mod__求余運算
* __pow__稱方
更多介紹請查看:[http://www.w3cschool.cc/python/python-object.html](/python/python-object.html)
- Python 基礎教程
- Python 簡介
- Python 環境搭建
- Python 基礎語法
- Python 變量類型
- Python 運算符
- Python 條件語句
- Python 循環語句
- Python While循環語句
- Python for 循環語句
- Python 循環嵌套
- Python break 語句
- Python continue 語句
- Python pass 語句
- Python 數字
- Python 字符串
- Python 列表(Lists)
- Python 元組
- Python 字典(Dictionary)
- Python 日期和時間
- Python 函數
- Python 模塊
- Python 文件I/O
- Python 異常處理
- Python 高級教程
- Python 面向對象
- Python 正則表達式
- Python CGI編程
- Python 使用SMTP發送郵件
- Python 多線程
- Python 2.x與3??.x版本區別
- Python IDE
- Python JSON
- Python3 教程
- Python3 基礎語法
- Python3 基本數據類型
- Python3 解釋器
- Python3 注釋
- Python3 數字運算
- Python3 字符串
- Python3 列表
- Python3 編程第一步
- Python3 條件控制
- Python3 循環
- Python3 函數
- Python3 數據結構
- Python3 模塊
- Python3 輸入和輸出
- Python3 錯誤和異常
- Python3 類
- Python3 標準庫概覽
- 免責聲明