一文講全了Python類和對象內容

{"type":"doc","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"​​","attrs":{}},{"type":"link","attrs":{"href":"https://bbs.huaweicloud.com/blogs/279968?utm_source=infoq&utm_medium=bbs-ex&utm_campaign=other&utm_content=content","title":"","type":null},"content":[{"type":"text","text":"【本期推薦】你能否在直播間搶到秒殺商品的關鍵不僅僅是手速,還有它:大規模超低時延直播,點擊就看華爲雲音視頻的硬核解決方案。","attrs":{}}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/3b/3b9d7fcfbbc5d3057685bd34e880de08.jpeg","alt":null,"title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"blockquote","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"摘要:這是一個關於 Python 類和對象的全部內容。","attrs":{}}]}],"attrs":{}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"本文分享自華爲雲社區","attrs":{}},{"type":"link","attrs":{"href":"https://bbs.huaweicloud.com/blogs/279955?utm_source=infoq&utm_medium=bbs-ex&utm_campaign=other&utm_content=content","title":"","type":null},"content":[{"type":"text","text":"《從零開始學python| Python 類和對象—面向對象編程》","attrs":{}}]},{"type":"text","text":",原文作者:Yuchuan 。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"Python 在活躍開發人員方面將超過其他語言之後,Python 開發人員的需求只會增長。 Python 遵循面向對象的編程範式。它處理聲明 python 類,從它們創建對象並與用戶交互。在面向對象的語言中,程序被分成獨立的對象,或者你可以說成幾個小程序。每個對象代表應用程序的不同部分,它們可以相互通信。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"在這個python類博客中,您將按以下順序瞭解類和對象的各個方面:","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"What is a Python Class?","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"Methods and Attributes in a class","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"What are Objects?","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"OOPs Concepts:","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"Inheritance","attrs":{}}]}]}],"attrs":{}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"Polymorphism","attrs":{}}]}]}],"attrs":{}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"Abstraction","attrs":{}}]}]}],"attrs":{}},{"type":"heading","attrs":{"align":null,"level":1},"content":[{"type":"text","text":"什麼是 Python 類?","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"python 中的類是創建特定對象的藍圖。它使您可以以特定方式構建軟件。問題來了,怎麼辦?類允許我們以一種易於重用的方式對我們的數據和函數進行邏輯分組,並在需要時進行構建。考慮下圖。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/92/926fcf3e185e2128bcb23edd9f59a427.jpeg","alt":null,"title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"在第一張圖片 (A) 中,它代表了一個可以被視爲Class的房子的藍圖。使用相同的藍圖,我們可以創建多個房屋,這些可以視爲Objects。使用類,您可以爲您的程序添加一致性,以便以更簡潔、更有效的方式使用它們。屬性是通過點表示法訪問的數據成員(類變量和實例變量)和方法。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"類變量是一個類的所有不同對象/實例共享的變量。","attrs":{}}]}]}],"attrs":{}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"實例變量是每個實例唯一的變量。它是在方法內部定義的,並且只屬於類的當前實例。","attrs":{}}]}]}],"attrs":{}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"方法也稱爲函數,它們在類中定義並描述對象的行爲。","attrs":{}}]}]}],"attrs":{}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"現在,讓我們繼續前進,看看它在 PyCharm 中是如何工作的。要開始,首先看一下 python 類的語法。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"語法:","attrs":{}}]},{"type":"codeblock","attrs":{"lang":"text"},"content":[{"type":"text","text":"class Class_name:\nstatement-1\n.\n.\nstatement-N ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"在這裏,“class”語句創建了一個新的類定義。類的名稱緊跟 在python中的關鍵字“class”之後,後跟一個冒號。要在 python 中創建一個類,請考慮以下示例:","attrs":{}}]},{"type":"codeblock","attrs":{"lang":"text"},"content":[{"type":"text","text":"class employee:\n pass\n #no attributes and methods\n emp_1=employee()\n emp_2=employee()\n #instance variable can be created manually\n emp_1.first='aayushi'\n emp_1.last='Johari'\n emp_1.email='[email protected]'\n emp_1.pay=10000\n \nemp_2.first='test'\n emp_2.last='abc'\n emp_2.email='[email protected]'\n emp_2.pay=10000\n print(emp_1.email)\n print(emp_2.email) ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"輸出","attrs":{}}]},{"type":"codeblock","attrs":{"lang":"text"},"content":[{"type":"text","text":"[email protected]\[email protected]","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"現在,如果我們不想手動設置這些變量怎麼辦。你會看到很多代碼,而且很容易出錯。所以爲了讓它自動,我們可以使用“init”方法。爲此,讓我們瞭解一下 Python 類中的方法和屬性到底是什麼。","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":1},"content":[{"type":"text","text":"Python 類中的方法和屬性","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"如果沒有一些功能,現在創建一個類是不完整的。因此,可以通過設置各種屬性來定義功能,這些屬性充當與這些屬性相關的數據和功能的容器。python 中的函數也稱爲方法。談到init方法,它是一個特殊的函數,每當實例化該類的新對象時都會調用它。您可以將其視爲 initialize方法,或者如果您來自任何其他面向對象的編程背景,例如 C++、Java 等,則可以將其視爲構造函數。現在當我們在類中設置方法時,它們會自動接收實例。讓我們繼續使用 python 類並使用此方法接受名字、姓氏和薪水。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"text"},"content":[{"type":"text","text":"class employee:\n def __init__(self, first, last, sal):\n self.fname=first\n self.lname=last\n self.sal=sal\n self.email=first + '.' + last + '@company.com'\n \nemp_1=employee('aayushi','johari',350000)\nemp_2=employee('test','test',100000)\nprint(emp_1.email)\nprint(emp_2.email) ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"現在在我們的“init”方法中,我們設置了這些實例變量(self、first、last、sal)。Self 是實例,這意味着每當我們寫self.fname=first 時,它與emp_1.first='aayushi' 相同。然後我們創建了員工類的實例,我們可以在其中傳遞 init 方法中指定的值。此方法將實例作爲參數。現在它將自動完成,而不是手動完成。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"接下來,我們希望能夠執行某種操作。爲此,我們將向 此類添加一個 方法。假設我想要顯示員工全名的功能。因此,讓我們實際實現這一點。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"text"},"content":[{"type":"text","text":"class employee:\n def __init__(self, first, last, sal):\n self.fname=first\n self.lname=last\n self.sal=sal\n self.email=first + '.' + last + '@company.com'\n \n def fullname(self):\n return '{}{}'.format(self.fname,self.lname)\n \nemp_1=employee('aayushi','johari',350000)\nemp_2=employee('test','test',100000)\nprint(emp_1.email)\nprint(emp_2.email)\nprint(emp_1.fullname())\nprint(emp_2.fullname()) \n","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"輸出","attrs":{}}]},{"type":"codeblock","attrs":{"lang":"text"},"content":[{"type":"text","text":"[email protected] \n [email protected] \n aayushijohari \n TESTTEST","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"正如你在上面看到的,我在一個類中創建了一個名爲“全名”的方法。因此,python 類中的每個方法都會自動將實例作爲第一個參數。現在在這個方法中,我編寫了打印全名並返回這個而不是 emp_1 名字和姓氏的邏輯。接下來,我使用了“self”,以便它適用於所有實例。因此每次都打印這個,我們使用一個方法。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"繼續使用 Python 類,有一些變量在類的所有實例之間共享。這些被稱爲類變量。實例變量對於每個實例都可以是唯一的,例如姓名、電子郵件、sal 等。複雜嗎?讓我們通過一個例子來理解這一點。請參閱下面的代碼以瞭解工資的年度增長情況。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"text"},"content":[{"type":"text","text":"class employee:\n perc_raise =1.05\n def __init__(self, first, last, sal):\n self.fname=first\n self.lname=last\n self.sal=sal\n self.email=first + '.' + last + '@company.com'\n \n def fullname(self):\n return '{}{}'.format(self.fname,self.lname)\n def apply_raise(self):\n self.sal=int(self.sal*1.05)\n \nemp_1=employee('aayushi','johari',350000)\nemp_2=employee('test','test',100000)\n \nprint(emp_1.sal)\nemp_1.apply_raise()\nprint(emp_1.sal) ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"輸出","attrs":{}}]},{"type":"codeblock","attrs":{"lang":"text"},"content":[{"type":"text","text":"350000\n367500","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"正如你在上面看到的,我先打印了工資,然後應用了 1.5% 的增長。爲了訪問這些類變量,我們需要通過類或類的實例來訪問它們。現在,讓我們瞭解 Python 類中的各種屬性。","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":1},"content":[{"type":"text","text":"Python 類中的屬性","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"Python 中的屬性定義了對象、元素或文件的屬性。有兩種類型的屬性:","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"內置類屬性: Python類中存在各種內置屬性。例如_dict_、","attrs":{}},{"type":"text","marks":[{"type":"italic","attrs":{}}],"text":"doc","attrs":{}},{"type":"text","text":"、_name _ 等。讓我舉同樣的例子,我想查看employee1 的所有鍵值對。 爲此,您可以簡單地編寫以下包含類命名空間的語句:","attrs":{}}]}]}],"attrs":{}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"打印(emp_1.dict)","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"執行之後,你會得到這樣的輸出:{'fname':'aayushi', 'lname': 'johari', 'sal': 350000, 'email':'[email protected]'}","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"用戶定義的屬性:屬性是在類定義中創建的。我們可以爲類的現有實例動態創建新屬性。屬性也可以綁定到類名。接下來,我們有public、protected 和private屬性。讓我們詳細瞭解它們:","attrs":{}}]}]}],"attrs":{}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/38/389df808eae65d62b9943003bd40354b.jpeg","alt":null,"title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"​","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"接下來,讓我們瞭解一個python類中最重要的組件,即Objects。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/90/90b1ac4e9df5f89a55340853416c442e.jpeg","alt":null,"title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"正如我們上面討論的,一個對象可以用來訪問不同的屬性。它用於創建類的實例。實例是在運行時創建的類的對象。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"給你一個快速瀏覽,對象主要是看到周圍的一切。例如:狗是動物類的對象,我是人類類的對象。同樣,同一個電話類可以有不同的對象。 這與我們已經討論過的函數調用非常相似。讓我們通過一個例子來理解這一點:","attrs":{}}]},{"type":"codeblock","attrs":{"lang":"text"},"content":[{"type":"text","text":"class MyClass:\n \n def func(self):\n print('Hello')\n \n# create a new MyClass\nob = MyClass()\nob.func() ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"繼續使用 python 類,讓我們瞭解各種 OOP 概念。","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":1},"content":[{"type":"text","text":"面向對象的概念","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"OOPs 指的是 Python 中的面向對象編程。好吧,Python 並不是完全面向對象的,因爲它包含一些過程函數。現在,您一定想知道過程編程和麪向對象編程之間有什麼區別。爲了消除您的疑問,在過程式編程中,整個代碼都被寫入一個長過程,即使它可能包含函數和子例程。由於數據和邏輯混合在一起,因此無法管理。但是當我們談論面向對象編程時,程序被拆分爲獨立的對象或幾個小程序。每個對象代表應用程序的不同部分,這些部分有自己的數據和邏輯來在它們之間進行通信。例如,一個網站有不同的對象,如圖像、視頻等。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"面向對象編程包括Python類、對象、繼承、多態、抽象等概念,下面讓我們詳細瞭解這些主題。","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":1},"content":[{"type":"text","text":"Python 類:繼承","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"繼承允許我們從基類/父類繼承屬性和方法。這很有用,因爲我們可以創建子類並從父類中獲取所有功能。然後我們可以在不影響父類的情況下覆蓋和添加新功能。讓我們通過一個例子來理解父類和子類的概念。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/b5/b5bed2dd052bc4ed7573eef05572c260.jpeg","alt":null,"title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"​正如我們在圖像中看到的,孩子繼承了父親的屬性。同樣,在python中,有兩個類:","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"numberedlist","attrs":{"start":1,"normalizeStart":1},"content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":1,"align":null,"origin":null},"content":[{"type":"text","text":"父類(Super 或 Base 類)","attrs":{}}]}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"2.子類(子類或派生類)","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"繼承屬性的類稱爲子類,而繼承屬性的類稱爲父類。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"繼承是指創建包含其父類的專業化的子類的能力。它進一步分爲四種類型,即單繼承、多級繼承、分層繼承和多重繼承。請參考下圖以獲得更好的理解。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/38/38125c8ef866cec84a6609a457d0d743.jpeg","alt":null,"title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"​讓我們繼續使用 python 類並瞭解繼承是如何有用的。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"比如說,我想爲員工類型創建類。我將創建'developers' 和 'managers'作爲子類,因爲開發人員和經理都有姓名、電子郵件和薪水,所有這些功能都在員工類中。因此,我們不必複製子類的代碼,而是可以通過從員工那裏繼承來簡單地重用代碼。","attrs":{}}]},{"type":"codeblock","attrs":{"lang":"text"},"content":[{"type":"text","text":"class employee:\n num_employee=0\n raise_amount=1.04\n def __init__(self, first, last, sal):\n self.first=first\n self.last=last\n self.sal=sal\n self.email=first + '.' + last + '@company.com'\n employee.num_employee+=1\n def fullname (self):\n return '{} {}'.format(self.first, self.last)\n def apply_raise (self):\n self.sal=int(self.sal * raise_amount)\nclass developer(employee):\n pass\n \nemp_1=developer('aayushi', 'johari', 1000000)\nprint(emp_1.email)\n輸出- [email protected] ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"正如您在上面的輸出中看到的,employee 類的所有詳細信息都可以在 developer 類中找到。 現在,如果我想將開發人員的raise_amount 更改爲 10% 怎麼辦?讓我們看看它是如何實際完成的。","attrs":{}}]},{"type":"codeblock","attrs":{"lang":"text"},"content":[{"type":"text","text":"class employee:\n num_employee=0\n raise_amount=1.04\n def __init__(self, first, last, sal):\n self.first=first\n self.last=last\n self.sal=sal\n self.email=first + '.' + last + '@company.com'\n employee.num_employee+=1\n def fullname (self):\n return '{} {}'.format(self.first, self.last)\n def apply_raise (self):\n self.sal=int(self.sal* raise_amount)\n \nclass developer(employee):\n raise_amount = 1.10\n \nemp_1=developer('aayushi', 'johari', 1000000)\nprint(emp_1.raise_amount)\n輸出- 1.1 ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"如您所見,它已將工資增長百分比從 4% 更新爲 10%。 現在,如果我想再添加一個屬性,比如在我們的 init 方法中使用一種編程語言,但它在我們的父類中不存在。有什麼解決辦法嗎?是的!我們可以複製整個員工邏輯並這樣做,但它會再次增加代碼大小。因此,爲了避免這種情況,讓我們考慮以下代碼:","attrs":{}}]},{"type":"codeblock","attrs":{"lang":"text"},"content":[{"type":"text","text":"class employee:\n num_employee=0\n raise_amount=1.04\n def __init__(self, first, last, sal):\n self.first=first\n self.last=last\n self.sal=sal\n self.email=first + '.' + last + '@company.com'\n employee.num_employee+=1\n def fullname (self):\n return '{} {}'.format(self.first, self.last)\n def apply_raise (self):\n self.sal=int(self.sal* raise_amount)\nclass developer(employee):\n raise_amount = 1.10\n def __init__(self, first, last, sal, prog_lang):\n super().__init__(first, last, sal)\n self.prog_lang=prog_lang\n \nemp_1=developer('aayushi', 'johari', 1000000, 'python')\nprint(emp_1.prog_lang) ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"因此,只用一點點代碼,我就進行了更改。我使用了super.","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"init","attrs":{}},{"type":"text","text":"(first, last, pay) 它繼承了基類的屬性。 總之,繼承用於重用代碼並降低程序的複雜性。","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":1},"content":[{"type":"text","text":"Python 類:多態性","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"計算機科學中的多態性是爲不同的底層形式呈現相同界面的能力。實際上,多態意味着如果類 B 從類 A 繼承,它不必繼承關於類 A 的所有內容,它可以做一些與類 A 不同的事情。它最常用於處理繼承。Python 是隱式多態的,它能夠重載標準運算符,以便它們根據上下文具有適當的行爲。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"讓我們通過一個例子來理解:","attrs":{}}]},{"type":"codeblock","attrs":{"lang":"text"},"content":[{"type":"text","text":"class Animal:\n def __init__(self,name):\n self.name=name\n def talk(self):\n pass\nclass Dog(Animal):\n def talk(self):\n print('Woof')\nclass Cat(Animal):\n def talk(self):\n print('MEOW!')\nc= Cat('kitty')\nc.talk()\nd=Dog(Animal)\nd.talk() ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"輸出","attrs":{}}]},{"type":"codeblock","attrs":{"lang":"text"},"content":[{"type":"text","text":"Meow!\nWoof ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"接下來,讓我們轉向另一個面向對象的編程概念,即抽象。","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":1},"content":[{"type":"text","text":"Python 類:抽象","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"抽象用於通過建模適合問題的類來簡化複雜的現實。在這裏,我們有一個無法實例化的抽象類。這意味着您不能爲這些類創建對象或實例。它只能用於繼承您稱爲基類的某些功能。因此,您可以繼承功能,但同時,您無法創建此特定類的實例。讓我們通過下面的例子來理解抽象類的概念:","attrs":{}}]},{"type":"codeblock","attrs":{"lang":"text"},"content":[{"type":"text","text":"from abc import ABC, abstractmethod \n \nclass Employee(ABC):\n @abstractmethod\n \n def calculate_salary(self,sal):\n pass\n \nclass Developer(Employee):\n \n def calculate_salary(self,sal):\n finalsalary= sal*1.10\n return finalsalary\n \nemp_1 = Developer()\nprint(emp_1.calculate_salary(10000)) \n","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"輸出","attrs":{}}]},{"type":"codeblock","attrs":{"lang":"text"},"content":[{"type":"text","text":"11000.0 ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"正如你在上面的輸出中看到的,我們已經將基本工資增加到 10%,即現在的工資是 11000。現在,如果你真的繼續創建一個“Employee”類的對象,它會拋出一個錯誤,因爲 python 不會'不允許你創建抽象類的對象。但是使用繼承,您實際上可以繼承屬性並執行相應的任務。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"link","attrs":{"href":"https://bbs.huaweicloud.com/blogs?utm_source=infoq&utm_medium=bbs-ex&utm_campaign=other&utm_content=content","title":"","type":null},"content":[{"type":"text","text":"點擊關注,第一時間瞭解華爲雲新鮮技術~","attrs":{}}]}]}]}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章