一、前言

什麼是class,什麼是物件導向,class的繼承、封裝、多型是什麼?怎麼用。

一、介紹什麼是類(Class)一分鐘秒懂

二、告訴你怎麼用繼承、封裝及多型

 

YouTube:恩哥想當快樂社畜
https://youtube.com/channel/UCuj9oYDXt4D0mBgae4ODK-g

 

 

二、什麼是class類

簡單來說,就是物件(Object)的設計圖。這邊可以看到今天要設計一隻貓咪(Cat),我們就先畫出一隻貓咪的設計圖。

然後class裡面有屬性(Attribute)、建構子(Constructor)及方法(Method),來完成你的設計圖,

類別(Class) >> 你的設計圖

屬性(Attribute) >> 設計圖的變數,想像成貓咪的顏色變數我們定義Red

建構子(Constructor) >> 想像成class特有的方法(Method)

方法(Method) >> 設計圖的函數,想像設計圖要定義貓咪的一些動作

物件(Object) >> 把設計圖具體化,nask = Cat(),nash就是Cat類的具體化

# Cat的藍圖
class Cat:
    # 建構子
    def __init__(self):
        self.color = 'Red' # 屬性(Attribute)
        self.legs = 'Long' # 屬性(Attribute)
    
    # 方法(Method)
    def run(self):
        print('Cat is running')
        
# Leo的藍圖
class Leo:
    # 建構子
    def __init__(self):
        self.color = 'Black' # 屬性(Attribute)
        self.legs = 'Short' # 屬性(Attribute)
    
    # 方法(Method)
    def eat(self):
        print('Leo is eatting')
        

nask = Cat() # nask是Cat的具體化物件(object)
nask.run() # 使用物件裡面的方法

mary = Leo() # mary是Leo的具體化物件(object)
mary.eat() # 使用物件裡面的方法

 

三、物件導向類的基礎應用

以下分成 (1) 繼承(Inheritance)、(2) 封裝(Encapsulation)、(3) 多型(Polymorphism)來說!

(1) 繼承(Inheritance)

很簡單的概念,跟現實的繼承一樣,你爸留給你的你也可以用!

繼承還分成很多種我們來看看吧!

class Father:
    def __init__(self):
        self.head = 'Big'
    
    def work(self):
        print('work hard')


# 此時Father就是Son的父類
# 也可以說Son繼承Father類
class Son(Father):
    def __init__(self):
        self.color = 'Red'
        self.legs = 'Long'
    
    def play(self):
        print('play hard')
        
        
nask = Son()
nask.work() # 繼承後就可以使用父類的屬性及方法

a 方法覆寫(Method Overriding)

你會發現一個問題是,如果子類也有父類相同的屬性及方法怎辦,那就已子類會蓋過父類!

 

b 多層繼承(Multi-Level Inheritance)

Father類繼承了Grandfather類,而Son類又繼承了Father類,那麼Son就可以用Grandfather的屬性及方法,但這邊看到的Father與Grandfather都有self.work()方法,那麼Father繼承Grandfather,所以Father會覆蓋掉Grandfather的,也就是a點提到的方法覆寫(Method Overriding)

# Grandfather類
class Grandfather:
    def __init__(self):
        self.head = 'Big'
    
    def work(self):
        print('No work')


# Father繼承了Grandfather
class Father(Grandfather):
    def __init__(self):
        self.head = 'Big'
    
    def work(self):
        print('work hard')


# Grandfather繼承了Son
class Son(Father):
    def __init__(self):
        self.color = 'Red'
        self.legs = 'Long'
    
    def play(self):
        print('play hard')
        
        
nask = Son()
nask.work() # work hard
nask.play() # play hard

 

c 多重繼承(Multiple Inheritance)

class Son(Father, Mother),可以看到一次繼承了兩個類,但如果有相同的方法,

那麼就看誰放前面了!!!

class Mother:
    def __init__(self):
        self.head = 'Big'
    
    def work(self):
        print('No work')


class Father:
    def __init__(self):
        self.head = 'Big'
    
    def work(self):
        print('work hard')
        
# Son繼承Father及Monther類
class Son(Father, Mother):
    def __init__(self):
        self.color = 'Red'
        self.legs = 'Long'
    
    def play(self):
        print('play hard')
        
        
nask = Son()
nask.work() # No work
nask.play() # play hard

 

(2) 封裝(Encapsulation)

如果類裡面的方法只想在類裡面呼叫,但不想在物件被呼叫,那麼就有封裝的概念出來了。只要在方法或是屬性在定義時加上__兩條線,那麼就可以了!!

舉實際例子來說:提款時雖然在ATM密碼(屬性)送出去了,而且在裡面運作驗證,但沒辦法把密碼吐回來看。

私有屬性(Private Attribute) >> 封裝屬性

私有方法(Private Method) >> 封裝方法

class Son:
    def __init__(self):
        self.color = 'Red'
        self.legs = 'Long'
        self.__IQ = '300'
    
    def play(self):
        print('play hard')
    
    def hungry(self):
        self.__eat()
    
    def __eat(self):
        print('good')
        
        

nask = Son()
nask.play() # play hard
nask.hungry() # good
nask.__eat() # 'Son' object has no attribute '__eat' 只能在類裡面自己呼叫用
nask.__IQ() # 'Son' object has no attribute '__IQ' 屬性也是一樣

 

(3) 多型(Polymorphism)

不同的類被具體化成為物件之後,即使同一個方法但來自不同類就可以分別作用,就是多型的概念。

# Cat的藍圖
class Cat:
    # 建構子
    def __init__(self):
        self.color = 'Red' # 屬性(Attribute)
        self.legs = 'Long' # 屬性(Attribute)
    
    # 方法(Method)
    def eat(self):
        print('Cat is eatting')
        
# Leo的藍圖
class Leo:
    # 建構子
    def __init__(self):
        self.color = 'Black' # 屬性(Attribute)
        self.legs = 'Short' # 屬性(Attribute)
    
    # 方法(Method)
    def eat(self):
        print('Leo is eatting')
        

nask = Cat() 
nask.eat() # Cat is eatting

mary = Leo() 
mary.eat() # Leo is eatting

 

四、後記

如果出現子類覆寫覆類方法的情況下,還想使用父的方法那麼就需要用到super函數!

會常class裡面常常看到__init__至於什麼意思呢?

【Python基礎】什麼是self?什麼是__init__?:看完文章馬上會

arrow
arrow
    創作者介紹
    創作者 恩哥Python 的頭像
    恩哥Python

    恩哥Python量化教室-零基礎也能學會Python

    恩哥Python 發表在 痞客邦 留言(1) 人氣()