Python class類別的使用
14 min readJun 8, 2021
話不多說,直接進入重點。
Outline:
- 定義Class類別
- 在類別中的使用函數(Method)
- 類別中定義「屬性」(Property)
- 在類別(Method)中又呼叫其他類別(Method)
- 設定類別(Method)與屬性(Property)的私有與公開
- 繼承(重要!!)
- 多重繼承
- 呼叫父類別的函式(Method)或屬性(Property)
*定義Class類別
class FirstClass(object): # 繼承 python最上層的object類別
def __init__(self): # 稱為 constuctor,建構方法
print("hello")hi = FirstClass() # 建立一個名為hi的類別--------------------------------------------------------------------Output:
hello
建構方法是甚麼呢?
在Java中也可以看到類似的概念,其實就是在建立一個物件的時候,要給他一個初始的狀態。而建構方法,是來完成這項任務的手段。
另一種初始化的方式,「自訂初始化的值」。
class FirstClass(object):
def __init__(self, name): # constructor
print("hello " + str(name))
hi = MyClass('Jack') # 建立一個名為hi的類別,並傳入參數--------------------------------------------------------------------Output:
hello Jack
*在類別中的使用函數(Method)
class FirstClass(object):
def __init__(self, name):
print("hello " + str(name))
def fun1(self): # 定義一個名為fun1的函式
print("This is function1") # 定義一個名為fun2的函式,因為後面有多宣告,代表此函式可以傳入參數
def fun2(self, num1 = 0, num2=0):
return (num1 * 2) + num2
hi = MyClass('Jack')
hi.fun1()
print(hi.fun2(1, 2)) # 傳入1, 2參數進去
# print(hi.fun2(1, 2))== print(hi.fun2(num1=1, num2=2))--------------------------------------------------------------------Output:
hello Jack
This is function1
4
如果再fun2裡面不傳參數的話,則會默認那個數值是一開始預設的num1=0, num2=0。
*注意!!
函式(def)的第一個參數必須加上self,表示說我是使用目前的物件。(如果不是很懂沒關係,看之後的範例應該就可以理解它的功用了)
*類別中的「屬性」(Property)
屬性描述此物件的性質。以下方為例,類別為People,描述「人」這項類別有哪些特徵,身高體重等等。
class People(object):
height = 150 # Property
weight = 50 # Property
def __init__(self, h, w):
self.height = h
self.weight = w
def BMI(self):
bmi = self.weight / (self.height / 100)**2
print(bmi)
bill = People(170, 60)
bill.BMI()
print(bill.height)
print(bill.weight)--------------------------------------------------------------------Output:
20.761245674740486
170
60
Note:
def __init__(self, h, w):
self.height = h
self.weight = w# 這邊我們不是直接把height = h, weight = w,而是用self.height = h,在這邊的self就代表說,我們是用某個建立好的物件將他的,某某.height = h。
*在類別(Method)中又呼叫其他類別(Method)
class People(object):
height = 150
weight = 50
name = 'unknown'
def __init__(self, h, w, n):
self.height = h
self.weight = w
self.name = str(n)
def BMI(self):
bmi = self.weight / (self.height / 100)**2
self.Print(bmi) # 在這邊直接呼叫下面的Print函式
def Print(self, num):
print(str(self.name) + "\nheight: " + str(self.height) + "\nweight: " + str(self.weight))
print("His BMI is " + str(num))
bill = People(170, 60, 'bill')
bill.BMI()--------------------------------------------------------------------Output:
20.761245674740486
170
60
*設定類別(Method)與屬性(Property)的私有與公開
這邊的私有與公開(在C++中,稱為private, public),代表外界不能隨意地修改,或是去使用它。如果硬去訪問它的話會出現error,請看下面範例。
class People(object):
height = 150
weight = 50
__name = 'unknown'
def __init__(self, h, w, n):
self.height = h
self.weight = w
self.__name = str(n) # 設定私有屬性
def __BMI(self): # 設定私有函式(Method)
bmi = self.weight / (self.height / 100)**2
print(bmi)
def printHeight(self):
print(self.height)
def printWeight(self):
print(self.weight)
def __printName(self):
print(self.__name)
bill = People(170, 60, 'bill')--------------------------------------------------------------------bill.BMI()
--------------------------------------------------------------------Output: AttributeError: 'People' object has no attribute 'BMI'
訪問到不該訪問的類別
----------------------------------------------------------------------------------------------------------------------------------------bill.name()
--------------------------------------------------------------------Output: AttributeError: 'People' object has no attribute 'name'
訪問到不該訪問的類別--------------------------------------------------------------------bill.printHeight()
bill.printWeight()
--------------------------------------------------------------------Output:
170
60
*繼承
這邊來舉一個比較現實的範例,用財產來舉例。爸爸身價有多少,兒子一出生就有多少財產。
class Father(object):
netWorth = 10**6
house = 10
company = 2
name = 'unknown'
def __init__(self, m, h, c, n):
self.money = m
self.house = h
self.company = c
self.name = str(n)
def printNetWorth(self):
print("Net Worth: " + str(self.netWorth))
def printHouse(self):
print("House Number: " + str(self.house))
def printComany(self):
print("Company Number: " + str(self.company))
class Child(Father): # 這邊原本放的是object,現在改為要繼承的class
def __init__(self, name):
print("Child: ", str(name))
bill = Child('bill') # 這邊建立的是兒子的物件
bill.printNetWorth() # 兒子此物件的身價可以直接拿爸爸的來使用
bill.printHouse() # 兒子此物件的房產可以直接拿爸爸的來使用--------------------------------------------------------------------Output:
Child: bill
Net Worth: 1000000
House Number: 10
*多重繼承
沒想到的是,bill深藏不露,媽媽居然也是個超級有錢人,身價甚至比爸爸還要高。小孩可以繼承的更多財產了。
class Father(object):
DadNetWorth = 10**6
DadHouse = 10
DadCompany = 2
DadName = 'Bear'
def __init__(self,n):
self.DadName = str(n)
def printNetWorthFromDad(self):
print("Dad Net Worth: " + str(self.DadNetWorth))
def printHouseFromDad(self):
print("Dad House Number: " + str(self.DadHouse))
def printComanyFromDad(self):
print("Dad Company Number: " + str(self.DadCompany))
class Mother(object):
MomNetWorth = 10 ** 9
MomHouse = 20
MomCompany = 10
MomName = 'Anna'
def __init__(self, n):
self.MomName = str(n)
def printNetWorthFromMom(self):
print("Mom Net Worth: " + str(self.MomNetWorth))
def printHouseFromMom(self):
print("Mom House Number: " + str(self.MomHouse))
def printComanyFromMom(self):
print("Mom Company Number: " + str(self.MomCompany))
class Child(Father, Mother): # 這邊原本放的是object,現在改為要繼承的class
def __init__(self, name):
print("Child: ", str(name))
def func(self):
super(Child, self).printComanyFromMom()
super(Child, self).printComanyFromDad()
def DadandMom(self):
print("His father is " + super(Child, self).DadName)
print("His Mother is " + super(Child, self).MomName)
bill = Child('bill')
bill.DadandMom()
bill.func()
bill = Child('bill')
bill.printNetWorthFromDad()
bill.printNetWorthFromMom()
bill.printComanyFromDad()
bill.printComanyFromMom()--------------------------------------------------------------------Output:
Child: bill
Dad Net Worth: 1000000
Mom Net Worth: 1000000000
Dad Company Number: 2
Mom Company Number: 10
*呼叫父類別的函式(Method)與屬性(Property)
super(自己類別的名稱, self).要呼叫的「類別」 或 「屬性」)
class Father(object):
DadNetWorth = 10**6
DadHouse = 10
DadCompany = 2
DadName = 'Bear'
def __init__(self,n):
self.DadName = str(n)
def printNetWorthFromDad(self):
print("Dad Net Worth: " + str(self.DadNetWorth))
def printHouseFromDad(self):
print("Dad House Number: " + str(self.DadHouse))
def printComanyFromDad(self):
print("Dad Company Number: " + str(self.DadCompany))
class Mother(object):
MomNetWorth = 10 ** 9
MomHouse = 20
MomCompany = 10
MomName = 'Anna'
def __init__(self, n):
self.MomName = str(n)
def printNetWorthFromMom(self):
print("Mom Net Worth: " + str(self.MomNetWorth))
def printHouseFromMom(self):
print("Mom House Number: " + str(self.MomHouse))
def printComanyFromMom(self):
print("Mom Company Number: " + str(self.MomCompany))
class Child(Father, Mother): # 這邊原本放的是object,現在改為要繼承的class
def __init__(self, name):
print("Child: ", str(name))
def func(self):
super(Child, self).printComanyFromMom() # !!重點!!
super(Child, self).printComanyFromDad() # !!重點!!
def DadandMom(self):
print("His father is " + super(Child, self).DadName)#!!重點!!
print("His Mother is " + super(Child, self).MomName)#!!重點!!
bill = Child('bill')
bill.DadandMom()
bill.func()--------------------------------------------------------------------Output:
Child: bill
His father is Bear
His Mother is Anna
Mom Company Number: 10
Dad Company Number: 2
希望能為各位有所幫助,那我們就下次見吧~~