欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

深入講解Python中面向?qū)ο缶幊痰南嚓P(guān)知識

 更新時間:2015年05月25日 09:57:34   投稿:goldensun  
這篇文章主要介紹了深入講解Python中面向?qū)ο缶幊痰南嚓P(guān)知識,是Python入門學習中的基礎(chǔ)知識,需要的朋友可以參考下

 Python從第一天開始就是面向?qū)ο蟮恼Z言。正因為如此,創(chuàng)建和使用類和對象是非常地容易。本章將幫助您在使用Python面向?qū)ο缶幊痰募夹g(shù)方面所有提高。

如果沒有任何以往面向?qū)ο?OO)的編程的經(jīng)驗,那么可能要了解一些基本的入門課程就可以了,或者至少某種形式的教程,讓你有了解基本概念。

但是,這里會比較少地介紹面向?qū)ο缶幊?OOP):
OOP術(shù)語概述

  •     類: 用戶定義的原型對象,它定義了一套描述類的任何對象的屬性。屬性是數(shù)據(jù)成員(類變量和實例變量)和方法,通過點符號訪問。
  •     類變量:這是一個類的所有實例共享的變量。類變量在類,但外面的任何類的方法定義。類變量不被用作經(jīng)常作為實例變量。
  •     數(shù)據(jù)成員:保存與類和對象關(guān)聯(lián)的數(shù)據(jù)的類變量或?qū)嵗兞俊?/li>
  •     函數(shù)重載:一個以上的行為特定功能的分配。執(zhí)行的操作所涉及的對象(自變量)的類型不同而不同。
  •     實例變量:所定義的方法內(nèi),只屬于一個類的當前實例的變量。
  •     繼承:類的特點,即都是由它派生其他類的轉(zhuǎn)移。
  •     實例:某一類的一個單獨對象。屬于類Circle一個obj對象,例如,是類Circle的一個實例。
  •     實例化:創(chuàng)建一個類的實例。
  •     Method : 一種特殊的函數(shù),函數(shù)在類定義中定義。
  •     對象:這是由它的類中定義的數(shù)據(jù)結(jié)構(gòu)的唯一實例。一個對象包括兩個數(shù)據(jù)成員(類變量和實例變量)和方法。
  •     運算符重載:一個以上的函數(shù)功能,特定的操作符分配。

創(chuàng)建類:

類語句將創(chuàng)建一個新的類定義。類的名稱緊跟在關(guān)鍵字class后跟一個冒號,如下所示:

class ClassName:
  'Optional class documentation string'
  class_suite

    類有一個文檔字符串,它可以通過類名.__ doc__訪問。

    class_suite由所有定義的類成員,數(shù)據(jù)屬性與函數(shù)組件的語句。

例子

下面是一個簡單的Python類的例子:

class Employee:
  'Common base class for all employees'
  empCount = 0

  def __init__(self, name, salary):
   self.name = name
   self.salary = salary
   Employee.empCount += 1
  
  def displayCount(self):
   print "Total Employee %d" % Employee.empCount

  def displayEmployee(self):
   print "Name : ", self.name, ", Salary: ", self.salary

    empCount是一個類變量,其值將是這個類的所有實例共享。這可以從類中或外部進行訪問,訪問形式為 Employee.empCount。

    第一個方法__init__()是一種特殊的方法,這就是所謂的類構(gòu)造函數(shù)或當創(chuàng)建該類的一個新實例Python調(diào)用的初始化方法。

    聲明就像正常函數(shù)中一樣,不同的是第一個參數(shù)到每個方法是類的方法。 Python增加了self參數(shù)列表;不需要把調(diào)用的方法都它列入。

創(chuàng)建實例對象:

要創(chuàng)建一個類的實例,調(diào)用類名并傳遞任何參數(shù)給__init__方法接收。

"This would create first object of Employee class"
emp1 = Employee("Zara", 2000)
"This would create second object of Employee class"
emp2 = Employee("Manni", 5000)

訪問屬性:

可以訪問使用點運算符來訪問對象的屬性。而類變量使用類名來訪問,如下所示:

emp1.displayEmployee()
emp2.displayEmployee()
print "Total Employee %d" % Employee.empCount

現(xiàn)在,把所有的概念放在一起:

#!/usr/bin/python

class Employee:
  'Common base class for all employees'
  empCount = 0

  def __init__(self, name, salary):
   self.name = name
   self.salary = salary
   Employee.empCount += 1
  
  def displayCount(self):
   print "Total Employee %d" % Employee.empCount

  def displayEmployee(self):
   print "Name : ", self.name, ", Salary: ", self.salary

"This would create first object of Employee class"
emp1 = Employee("Zara", 2000)
"This would create second object of Employee class"
emp2 = Employee("Manni", 5000)
emp1.displayEmployee()
emp2.displayEmployee()
print "Total Employee %d" % Employee.empCount

當執(zhí)行上面的代碼,產(chǎn)生以下結(jié)果:

Name : Zara ,Salary: 2000
Name : Manni ,Salary: 5000
Total Employee 2

在任何時候可以添加,刪除或修改類和對象的屬性:

emp1.age = 7 # Add an 'age' attribute.
emp1.age = 8 # Modify 'age' attribute.
del emp1.age # Delete 'age' attribute.

除了使用正常的語句來訪問屬性,可以使用以下函數(shù):

  •     getattr(obj, name[, default]) : 訪問對象的屬性。
  •     hasattr(obj,name) : 檢查一個屬性是否存在。
  •     setattr(obj,name,value) : 設置一個屬性。如果屬性不存在,那么它將被創(chuàng)建。
  •     delattr(obj, name) : 要刪除一個屬性。
hasattr(emp1, 'age')  # Returns true if 'age' attribute exists
getattr(emp1, 'age')  # Returns value of 'age' attribute
setattr(emp1, 'age', 8) # Set attribute 'age' at 8
delattr(empl, 'age')  # Delete attribute 'age'

內(nèi)置的類屬性:

每個Python類會繼續(xù)并帶有內(nèi)置屬性,他們可以使用點運算符像任何其他屬性一樣來訪問:

  •     __dict__ : 字典包含類的命名空間。
  •     __doc__ : 類的文檔字符串,或None如果沒有定義。
  •     __name__: 類名稱。
  •     __module__: 在類中定義的模塊名稱。此屬性是在交互模式其值為“__main__”。
  •     __bases__ : 一個可能是空的元組包含了基類,其在基類列表出現(xiàn)的順序。

對于上面的類,嘗試訪問這些屬性:

#!/usr/bin/python

class Employee:
  'Common base class for all employees'
  empCount = 0

  def __init__(self, name, salary):
   self.name = name
   self.salary = salary
   Employee.empCount += 1
  
  def displayCount(self):
   print "Total Employee %d" % Employee.empCount

  def displayEmployee(self):
   print "Name : ", self.name, ", Salary: ", self.salary

print "Employee.__doc__:", Employee.__doc__
print "Employee.__name__:", Employee.__name__
print "Employee.__module__:", Employee.__module__
print "Employee.__bases__:", Employee.__bases__
print "Employee.__dict__:", Employee.__dict__

當執(zhí)行上面的代碼,產(chǎn)生以下結(jié)果:

Employee.__doc__: Common base class for all employees
Employee.__name__: Employee
Employee.__module__: __main__
Employee.__bases__: ()
Employee.__dict__: {'__module__': '__main__', 'displayCount':
<function displayCount at 0xb7c84994>, 'empCount': 2, 
'displayEmployee': <function displayEmployee at 0xb7c8441c>, 
'__doc__': 'Common base class for all employees', 
'__init__': <function __init__ at 0xb7c846bc>}

銷毀對象(垃圾回收):

Python的刪除不需要的對象(內(nèi)建類型或類的實例),自動釋放內(nèi)存空間。由Python定期回收的內(nèi)存塊不再使用的過程被稱為垃圾收集。

Python的垃圾回收器在程序執(zhí)行過程中運行,當一個對象的引用計數(shù)為零時觸發(fā)。一個對象的引用計數(shù)改變?yōu)橹赶蛩淖儎e名的數(shù)量。

當它分配一個新的名字或放置在容器(列表,元組或字典)的對象的引用計數(shù)增加。當對象的引用計數(shù)減少使用 del 刪除,其基準被重新分配,或者它的引用超出范圍。當一個對象的引用計數(shù)變?yōu)榱?,Python會自動地收集它。

a = 40   # Create object <40>
b = a    # Increase ref. count of <40> 
c = [b]   # Increase ref. count of <40> 

del a    # Decrease ref. count of <40>
b = 100   # Decrease ref. count of <40> 
c[0] = -1  # Decrease ref. count of <40> 

當垃圾回收器銷毀孤立的實例,并回收其空間一般不會注意到。但是,一個類可以實現(xiàn)特殊方法__del__(),稱為析構(gòu)函數(shù)被調(diào)用時,該實例將被摧毀。這個方法可以用于清理所用的一個實例的任何非內(nèi)存資源。
例子:

__del__()析構(gòu)函數(shù)打印實例,它即將被銷毀的類名:

#!/usr/bin/python

class Point:
  def __init( self, x=0, y=0):
   self.x = x
   self.y = y
  def __del__(self):
   class_name = self.__class__.__name__
   print class_name, "destroyed"

pt1 = Point()
pt2 = pt1
pt3 = pt1
print id(pt1), id(pt2), id(pt3) # prints the ids of the obejcts
del pt1
del pt2
del pt3

當執(zhí)行上面的代碼,它產(chǎn)生以下結(jié)果:

3083401324 3083401324 3083401324
Point destroyed 

注意:理想情況下,應該定義類的單獨的文件,那么應該使用import語句將其導入主程序文件。詳細請查看Python- 模塊章節(jié),導入模塊和類的更多細節(jié)。
類繼承:

不用從頭開始,可以通過上面列出的括號父類的新類名后,從一個已經(jīng)存在的類派生它創(chuàng)建一個類。

子類繼承父類的屬性,可以使用父類的這些屬性,就好像它們是在子類中定義的一樣。子類也可以覆蓋父類的數(shù)據(jù)成員和方法。
語法

派生類的聲明很像它們的父類; 從基類的列表后給出類名繼承:

class SubClassName (ParentClass1[, ParentClass2, ...]):
  'Optional class documentation string'
  class_suite

例子

#!/usr/bin/python

class Parent:    # define parent class
  parentAttr = 100
  def __init__(self):
   print "Calling parent constructor"

  def parentMethod(self):
   print 'Calling parent method'

  def setAttr(self, attr):
   Parent.parentAttr = attr

  def getAttr(self):
   print "Parent attribute :", Parent.parentAttr

class Child(Parent): # define child class
  def __init__(self):
   print "Calling child constructor"

  def childMethod(self):
   print 'Calling child method'

c = Child()     # instance of child
c.childMethod()   # child calls its method
c.parentMethod()   # calls parent's method
c.setAttr(200)    # again call parent's method
c.getAttr()     # again call parent's method

當執(zhí)行上面的代碼,產(chǎn)生以下結(jié)果:

Calling child constructor
Calling child method
Calling parent method
Parent attribute : 200

類似的方式,可以按如下繼承多個父類的類:

復制代碼 代碼如下:
class A:        # define your class A
.....

class B:         # define your calss B
.....

class C(A, B):   # subclass of A and B
.....

可以使用issubclass()或isinstance()函數(shù)來檢查兩個類和實例的關(guān)系。

  •     issubclass(sub, sup) 如果給定的子類子確實是超sup的子類布爾函數(shù)返回true。
  •     isinstance(obj, Class) 如果obj是Class類的實例,或者是類的一個子類的實例布爾函數(shù)返回true

重寫方法:

可以覆蓋父類的方法。原因之一重寫父的方法,因為可能想在子類特殊或?qū)崿F(xiàn)不同的功能。
例子

#!/usr/bin/python

class Parent:    # define parent class
  def myMethod(self):
   print 'Calling parent method'

class Child(Parent): # define child class
  def myMethod(self):
   print 'Calling child method'

c = Child()     # instance of child
c.myMethod()     # child calls overridden method

當執(zhí)行上面的代碼,產(chǎn)生以下結(jié)果:

Calling child method

基礎(chǔ)重載方法:

下表列出了一些通用的功能,可以在類重寫中:

201552594506273.jpg (580×426)

 重載運算符:

假設要創(chuàng)建了一個Vector類來表示二維向量,當使用加運算符來增加他們發(fā)生了什么?最有可能是Python會理你。

可以,但是定義__add__方法在類中進行矢量相加,再加上操作符的行為會按預期:
例子:

#!/usr/bin/python

class Vector:
  def __init__(self, a, b):
   self.a = a
   self.b = b

  def __str__(self):
   return 'Vector (%d, %d)' % (self.a, self.b)
  
  def __add__(self,other):
   return Vector(self.a + other.a, self.b + other.b)

v1 = Vector(2,10)
v2 = Vector(5,-2)
print v1 + v2

當執(zhí)行上面的代碼,產(chǎn)生以下結(jié)果:

Vector(7,8)

數(shù)據(jù)隱藏:

對象的屬性可以是或可以不在類定義外部可見。對于這些情況,可以命名以雙下劃線前綴屬性,這些屬性將無法直接讓外部可視。
例子:

#!/usr/bin/python

class JustCounter:
  __secretCount = 0
 
  def count(self):
   self.__secretCount += 1
   print self.__secretCount

counter = JustCounter()
counter.count()
counter.count()
print counter.__secretCount

當執(zhí)行上面的代碼,產(chǎn)生以下結(jié)果:

1
2
Traceback (most recent call last):
 File "test.py", line 12, in <module>
  print counter.__secretCount
AttributeError: JustCounter instance has no attribute '__secretCount'

Python的保護成員通過內(nèi)部更改名稱以包含類名??梢栽L問這些屬性通過object._className__attrName。如果想更換最后一行,那么它會工作如下:

.........................
print counter._JustCounter__secretCount

當執(zhí)行上面的代碼,產(chǎn)生以下結(jié)果:

1
2
2

相關(guān)文章

  • python GUI庫圖形界面開發(fā)之PyQt5控件QTableWidget詳細使用方法與屬性

    python GUI庫圖形界面開發(fā)之PyQt5控件QTableWidget詳細使用方法與屬性

    這篇文章主要介紹了python GUI庫圖形界面開發(fā)之PyQt5控件QTableWidget詳細使用方法與屬性,需要的朋友可以參考下
    2020-02-02
  • Python代碼部署的三種加密方案

    Python代碼部署的三種加密方案

    本文主要介紹了Python代碼部署的三種加密方案,主要介紹了代碼混淆、代碼編譯、代碼打包這三種,具有一定的參考價值,感興趣的可以了解一下
    2023-02-02
  • 如何使用 Python 讀取文件和照片的創(chuàng)建日期

    如何使用 Python 讀取文件和照片的創(chuàng)建日期

    這篇文章主要介紹了如何使用 Python 讀取文件和照片的創(chuàng)建日期,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-09-09
  • 解決運行django程序出錯問題 ''str''object has no attribute''_meta''

    解決運行django程序出錯問題 ''str''object has no attribute''_meta''

    這篇文章主要介紹了解決運行django程序出錯問題 'str'object has no attribute'_meta',具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-07-07
  • python 判斷矩陣中每行非零個數(shù)的方法

    python 判斷矩陣中每行非零個數(shù)的方法

    今天小編就為大家分享一篇python 判斷矩陣中每行非零個數(shù)的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-01-01
  • python 簡單的股票基金爬蟲

    python 簡單的股票基金爬蟲

    最近基金非?;鸨芏嘣静煌顿Y、不理財人,也開始討論、參與買基金了。根據(jù)投資對象的不同,基金分為股票型基金、債券基金、混合型基金、貨幣基金。所以今天我們就來看看,這些基金公司都喜歡買那些公司的股票。
    2021-06-06
  • Python import導入上級目錄文件的方法

    Python import導入上級目錄文件的方法

    這篇文章主要介紹了Python import導入上級目錄文件,本文結(jié)合示例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2023-01-01
  • Python?httpstat命令行工具功能使用探索

    Python?httpstat命令行工具功能使用探索

    Python?httpstat是一個強大的命令行工具,用于深入了解HTTP請求的性能和狀態(tài)信息,本文將介紹Python?httpstat的基本用法、功能特性、示例代碼以及實際應用場景,幫助大家更好地理解和利用這個有用的工具
    2024-01-01
  • pyinstaller打包單個exe后無法執(zhí)行錯誤的解決方法

    pyinstaller打包單個exe后無法執(zhí)行錯誤的解決方法

    今天小編就為大家分享一篇pyinstaller打包單個exe后無法執(zhí)行錯誤的解決方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-06-06
  • 基于Pygame實現(xiàn)簡單的貪吃蛇游戲

    基于Pygame實現(xiàn)簡單的貪吃蛇游戲

    Pygame是一個專門用來開發(fā)游戲的Python模塊,主要用于開發(fā)、設計?2D?電子游戲。本文主要為大家介紹了通過Pygame制作一個簡單的貪吃蛇游戲,感興趣的同學可以關(guān)注一下
    2021-12-12

最新評論