Python基礎入門(類Class)
阿新 • • 發佈:2018-12-03
例項化:建立一個類的例項,類的具體物件。 類(Class): 用來描述具有相同的屬性和方法的物件的集合。它定義了該集合中每個物件所共有的屬性和方法。 方法:類中定義的函式。 類變數:類變數在整個例項化的物件中是公用的。類變數定義在類中且在函式體之外。類變數通常不作為例項變數使用。 資料成員:類變數或者例項變數用於處理類及其例項物件的相關的資料。 方法重寫:如果從父類繼承的方法不能滿足子類的需求,可以對其進行改寫,這個過程叫方法的覆蓋(override),也稱為方法的重寫。 例項變數:定義在方法中的變數,只作用於當前例項的類。 繼承:即一個派生類(derived class)繼承基類(base class)的欄位和方法。繼承也允許把一個派生類的物件作為一個基類物件對待。例如,有這樣一個設計:一個Dog型別的物件派生自Animal類,這是模擬"是一個(is-a)"關係(例圖,Dog是一個Animal)。 例項化:建立一個類的例項,類的具體物件。 物件:通過類定義的資料結構例項。物件包括兩個資料成員(類變數和例項變數)和方法。 In [11]: class Myclass: myname = "Arron" def fun(self): return "Kobe" # 例項化類 mc = Myclass() # 訪問類的屬性和方法 print(mc.myname) print(mc.fun()) Arron Kobe In [14]: # 類裡面預設有一個 __init__() 的特殊方法(構造方法),該方法在類例項化時會自動呼叫 class Myclass: def __init__(self,name,sex,age): self.n = name self.s = sex self.a = age x = Myclass("Kobe","boy",22) print(x.n,x.a,x.s) 3.0 -4.5 Kobe 22 boy In [22]: #self代表類的例項,而非類 #類的方法與普通的函式只有一個特別的區別——它們必須有一個額外的第一個引數名稱, 按照慣例它的名稱是 self。 #self 代表的是類的例項,代表當前物件的地址,而 self.class 則指向類 class Myclass: def Myshow(self): print(self,self.__class__) x = Myclass() x.Myshow() <__main__.Myclass object at 0x0000000004F71E48> <class '__main__.Myclass'> In [23]: #類定義 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 說: 我 %d 歲。" %(self.name,self.age)) # 例項化類 p = people('runoob',10,30) p.speak() runoob 說: 我 10 歲。 In [43]: #繼承 #父類 class animal: live = 1 def __init__(self,col,w,l): self.live = l self.color = col self.who = w if 1 == self.live: print("i am alive!") elif 0 == self.live: print("i am die") else: print("what the fuck!") def shit(self): print(self.who,"you look like shit!") #子類 class cat(animal): def __init__(self,col,w,l,r): animal.__init__(self,col,w,l) self.run = r #方法 def running(self): if self.run == 1: print("i am running now!") else: print("oh shit !") #重寫父類的方法 def shit(self): print(self.who,"I love you") #方法 def Color(self): print("I am a",self.color,self.who) cat1 = cat("Red","Cat",1,1) cat1.running() cat1.shit() cat1.Color() i am alive! i am running now! Cat I love you I am a Red Cat
#多繼承--菜鳥教程 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 說: 我 %d 歲。" %(self.name,self.age)) #單繼承示例 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 說: 我 %d 歲了,我在讀 %d 年級"%(self.name,self.age,self.grade)) #另一個類,多重繼承之前的準備 class speaker(): topic = '' name = '' def __init__(self,n,t): self.name = n self.topic = t def speak(self): print("我叫 %s,我是一個演說家,我演講的主題是 %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() #方法名同,預設呼叫的是在括號中排前地父類的方法 我叫 Tim,我是一個演說家,我演講的主題是 Python
菜鳥教程 類的私有屬性 __private_attrs:兩個下劃線開頭,宣告該屬性為私有,不能在類地外部被使用或直接訪問。在類內部的方法中使用時 self.__private_attrs。 類的方法 在類地內部,使用 def 關鍵字來定義一個方法,與一般函式定義不同,類方法必須包含引數 self,且為第一個引數,self 代表的是類的例項。 self 的名字並不是規定死的,也可以使用 this,但是最好還是按照約定是用 self。 類的私有方法 __private_method:兩個下劃線開頭,宣告該方法為私有方法,只能在類的內部呼叫 ,不能在類地外部呼叫。self.__private_methods。
類的專有方法:
__init__ : 建構函式,在生成物件時呼叫
__del__ : 解構函式,釋放物件時使用
__repr__ : 列印,轉換
__setitem__ : 按照索引賦值
__getitem__: 按照索引獲取值
__len__: 獲得長度
__cmp__: 比較運算
__call__: 函式呼叫
__add__: 加運算
__sub__: 減運算
__mul__: 乘運算
__div__: 除運算
__mod__: 求餘運算
__pow__: 乘方