python3:面向對象(多態和繼承、方法重載及模塊)

1、多態

同一個方法在不同的類中最終呈現出不同的效果,即為多態。

class Triangle:def __init__(self,width,height):self.width = widthself.height = heightdef getArea(self):area=self.width* self.height / 2return areaclass Square:def __init__(self,size):self.size = sizedef getArea(self):    # 同一個方法在不同的類中最終呈現出不同的效果,即為多態area = self.size * self.sizereturn areaa=Triangle(5,5)
print(a.getArea())
b=Square(5)
print(b.getArea())

執行效果如下:
1493082-20190723153232540-1614636829.png

2、繼承

(1)子類可以繼承父類所有的公有屬性和公有方法:

class Father:money = 1000000def drive(self):print('I can drive a car!')class Son(Father):passtom = Father()
print(tom.money)
tom.drive()print('#'*50)jerry=Son()
print(jerry.money)
jerry.drive()

執行后:
1493082-20190723153615569-902421162.png
(2)對于父類的私有屬性,子類不可以訪問。

(3)對于多繼承

多個父類的有相同的某個屬性,子類只繼承第一個父類的屬性。

3、方法重載

子類重寫父類的方法:


class Father:money = 1000000__money = 9999999def drive(self):print('I can drive a car!')class Son(Father):def drive(self):     #重載父類的方法drive()print('I can drive a tank!')Father.drive(self)    #在子類中直接調用父類的方法drive()def pay(self):print(self.money)tom = Father()
print(tom.money)
tom.drive()print('#'*50)jerry = Son()
jerry.drive()

執行后:
1493082-20190723153747431-1053743707.png

重載運算符

class Mylist:  #定義類Mylist__mylist = []def __init__(self,*args):self.mylist = []for arg in args:self.__mylist.append(arg)def __add__(self,x):   #定義操作+for i in range(0,len(self.__mylist)):  #依次遍歷列表,執行加操作self.__mylist[i] = self.__mylist[i]+xreturn self.__mylistdef __sub__(self,x):passdef __mul__(self,x):passdef __div__(self,x):passdef __mod__(self,x):passdef __pow__(self,x):passdef __len__(self,x):passdef show(self):   #顯示列表中的數值print(self.__mylist)if __name__ == '__main__':   #通過name的內置屬性l = Mylist(1,2,3,4,5)  #定義一個列表對象l+10l.show()

1493082-20190723154428269-203197630.png

4、模塊

從交互解釋器導入包,并調用包中的類和方法。如下:

>>> import claMylist
>>> dir(claMylist)
['Mylist', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__']
>>> import claMylist as cml
>>> ml=cml.Mylist(6,7,8,9,10)
>>> ml
<claMylist.Mylist object at 0x00000263F78D9240>
>>> ml.show()   #執行類中的方法show()
[6, 7, 8, 9, 10]
>>> ml+100
[106, 107, 108, 109, 110]
>>> x=ml+100   #將執行+操作后的值存放在x中
>>> x
[206, 207, 208, 209, 210]
>>>

轉載于:https://www.cnblogs.com/Paul-watermelon/p/11232219.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/392256.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/392256.shtml
英文地址,請注明出處:http://en.pswp.cn/news/392256.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

蠕變斷裂 ansys_如何避免范圍蠕變,以及其他軟件設計課程的辛苦學習方法

蠕變斷裂 ansysby Dror Berel由Dror Berel 如何避免范圍蠕變&#xff0c;以及其他軟件設計課程的辛苦學習方法 (How to avoid scope creep, and other software design lessons learned the hard way) 從數據科學的角度來看。 (From a data-science perspective.) You’ve got…

leetcode 674. 最長連續遞增序列

給定一個未經排序的整數數組&#xff0c;找到最長且 連續遞增的子序列&#xff0c;并返回該序列的長度。 連續遞增的子序列 可以由兩個下標 l 和 r&#xff08;l < r&#xff09;確定&#xff0c;如果對于每個 l < i < r&#xff0c;都有 nums[i] < nums[i 1] &a…

深入單例模式 java,深入單例模式四

Java代碼 privatestaticClass getClass(String classname)throwsClassNotFoundException {ClassLoader classLoader Thread.currentThread().getContextClassLoader();if(classLoader null)classLoader Singleton.class.getClassLoader();return(classLoader.loadClass(class…

linux下配置SS5(SOCK5)代理服務

SOCK5代理服務器 官網: http://ss5.sourceforge.net/ yum -y install gcc gcc-c automake make pam-devel openldap-devel cyrus-sasl-devel 一、安裝 # tar xvf ss5-3.8.9-5.tar.gz # cd ss5-3.8.9-5 # ./configure && make && make install 二、修改配置文…

劉備和諸葛亮鬧翻:無意說出蜀國滅亡的根源?

導讀&#xff1a;身為管理者&#xff0c;一件事情&#xff0c;自己做是滿分&#xff0c;別人做是八十分&#xff0c;寧可讓人去做八十分&#xff0c;自己也得跳出來看全局。緊抓大權不放&#xff0c;要么自己干到死&#xff0c;要么是敗于戰略&#xff01;&#xff01; 諸葛亮去…

mysql 時間推移_隨著時間的推移可視化COVID-19新案例

mysql 時間推移This heat map shows the progression of the COVID-19 pandemic in the United States over time. The map is read from left to right, and color coded to show the relative numbers of new cases by state, adjusted for population.該熱圖顯示了美國COVID…

leetcode 959. 由斜杠劃分區域(并查集)

在由 1 x 1 方格組成的 N x N 網格 grid 中&#xff0c;每個 1 x 1 方塊由 /、\ 或空格構成。這些字符會將方塊劃分為一些共邊的區域。 &#xff08;請注意&#xff0c;反斜杠字符是轉義的&#xff0c;因此 \ 用 “\” 表示。&#xff09;。 返回區域的數目。 示例 1&#x…

rcu寬限期_如何處理寬限期錯誤:靜默失敗不是一種選擇

rcu寬限期by Rina Artstain通過麗娜阿斯特斯坦 I’ve never really had much of an opinion about error handling. This may come as a shock to people who know me as quite opinionated (in a good way!), but yeah. If I was coming into an existing code base I just d…

描述符、迭代器、生成器

描述符&#xff1a;將某種特殊類型的類的實例指派給另一個類的屬性。 此處特殊類型的要求&#xff0c;至少實現”__set__(self , instance , owner)“、”__get__(self , instance , value)“、”__delete__(self , instance )“三個方法中的一個。 >>> class MyDecri…

php模擬表單提交登錄,PHP模擬表單的post請求實現登錄

stuid > $stuid,pwd > $pwd);$ch curl_init (); //初始化curlcurl_setopt ( $ch, CURLOPT_URL, $uri );curl_setopt ( $ch, CURLOPT_POST, 1 ); //使用post請求curl_setopt ( $ch, CURLOPT_HEADER, 0 );curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 );curl_setopt ( $…

去除list集合中重復項的幾種方法

因為用到list&#xff0c;要去除重復數據&#xff0c;嘗試了幾種方法。記錄于此。。。 測試數據&#xff1a; List<string> li1 new List<string> { "8", "8", "9", "9" ,"0","9"};List<string&g…

Crystal Reports第一張報表

新建一個網站項目&#xff0c;1. 設置數據庫 從服務器資源管理器中&#xff0c;數據連接中添加新連接&#xff0c;用Microsoft Access數據庫文件作為數據提供程序&#xff0c;連接上Crystal Reports的用例的數據庫Xtreme2. 創建新Crystal Reports報表 在工程項目中添加一個…

leetcode 1128. 等價多米諾骨牌對的數量

給你一個由一些多米諾骨牌組成的列表 dominoes。 如果其中某一張多米諾骨牌可以通過旋轉 0 度或 180 度得到另一張多米諾骨牌&#xff0c;我們就認為這兩張牌是等價的。 形式上&#xff0c;dominoes[i] [a, b] 和 dominoes[j] [c, d] 等價的前提是 ac 且 bd&#xff0c;或是…

海量數據尋找最頻繁的數據_尋找數據科學家的“原因”

海量數據尋找最頻繁的數據Start with “Why” - Why do we do the work we do?從“為什么”開始-我們為什么要做我們所做的工作&#xff1f; The question of “Why” is always a big question. Plus, it always makes you look smart in a meeting!“ 為什么 ”的問題始終是…

C語言中局部變量和全局變量 變量的存儲類別

C語言中局部變量和全局變量 變量的存儲類別(static,extern,auto,register) 局部變量和全局變量在討論函數的形參變量時曾經提到&#xff0c;形參變量只在被調用期間才分配內存單元&#xff0c;調用結束立即釋放。這一點表明形參變量只有在函數內才是有效的&#xff0c;離開該函…

營銷 客戶旅程模板_我如何在國外找到開發人員的工作:我從營銷到技術的旅程...

營銷 客戶旅程模板by Dimitri Ivashchuk由Dimitri Ivashchuk 我如何在國外找到開發人員的工作&#xff1a;我從營銷到技術的旅程 (How I got a developer job abroad: my journey from marketing to tech) In this post, I’ll go into the details of how I, a Ukrainian mar…

keepalive的作用

keepalive的作用是實現高可用,通過VIP虛擬IP的漂移實現高可用.在相同集群內發送組播包,master主通過VRRP協議發送組播包,告訴從主的狀態. 一旦主掛了從就選舉新的主,實現高可用 LVS專屬技能,通過配置文件控制lvs集群節點.對后端真實服務器進行健康檢查. 轉載于:https://www.cnb…

scrapy.Spider的屬性和方法

scrapy.Spider的屬性和方法 屬性: name:spider的名稱,要求唯一 allowed_domains:允許的域名,限制爬蟲的范圍 start_urls:初始urls custom_settings:個性化設置,會覆蓋全局的設置 crawler:抓取器,spider將綁定到它上面 custom_settings:配置實例,包含工程中所有的配置變量 logge…

php時間操作函數總結,基于php常用函數總結(數組,字符串,時間,文件操作)

數組:【重點1】implode(分隔,arr) 把數組值數據按指定字符連接起來例如&#xff1a;$arrarray(1,2,3,4);$strimplode(-,$arr);explode([分隔],arr)按指定規則對一個字符串進行分割&#xff0c;返回值為數組 別名joinarray_merge()合并一個或多個數組array_combine(array keys, …

kaggle比賽數據_表格數據二進制分類:來自5個Kaggle比賽的所有技巧和竅門

kaggle比賽數據This article was originally written by Shahul ES and posted on the Neptune blog.本文最初由 Shahul ES 撰寫&#xff0c; 并發布在 Neptune博客上。 In this article, I will discuss some great tips and tricks to improve the performance of your stru…