Skip to main content

OOP

OOP物件導向

  • 所有資料都是物件
  • 能夠提高軟體的重用姓,擴充性及維護姓
  • Encapsulation 封裝:外部程式碼只要透過統一介面來進行存取即可,而無需瞭解其中的運算過程
  • Inheritance 繼承:有父類別(或稱基底類別Base Class)及子類別(Sub Class)的階層關係。子類別會擁有父類別公開的屬性(Attribute)及方法(Method)
  • Polymorphism 多型:能夠讓程式碼的相依性不會那麼高,並且透過統一的介面來彈性擴充功能

Class類別

  • user-defined data structure that binds the data members and methods into a single unit
  • a blueprint or code template for object creation
  • 第一個英文字母要大寫
  • Class裡面的method第一個參數都要設為self
  • Class要取用內部attribute都要加上self.xxx
  • __init__函數:當Class被創建的時候會自動被執行1次,初始化的程式碼
class Person:
def __init__(self, name, sex, profession, study): //初始化的程式碼
#States (instance variables)
self.name = name
self.sex = sex
self.profession = profession
self.study = study

# Behavior (instance methods)
def show(self):
print('Name:',self.name,'Sex:',self.sex,'Profession:',self.profession)
def work(self):
print(self.name,' works as a ',self.profession)
def study(self):
print(self.name,' studies ',self.study,' hours per week')

# create instance of a class object
jessa = Person('Jessa', 'Female', 'Engineer',10)
# call methods
jessa.show()
jessa.work()
jessa.study()

Encapsulation封裝

  • 外部程式碼只要透過統一介面來進行存取即可,而無需瞭解其中的運算過程
  • 類別裡的attribute及method私有化
# Creating a class
class MyClass():
def __init__(self,fname,pwd=999):
self.fname = fname //can be accessed from the outside
self.__pwd = pwd //cannot be accessed from the outside
# Declaring public method
def fun(self): //can be called outside of this class
print("This is a Public method")
# Declaring private method
def __fun(self): //cannot be called outside of this class
print("This is a Private method")

x = MyClass('james')//create an instance of class
x.fname //can access fname
x.__pwd //cannot access this attribute. give error
x.fun //calling public method
x.__fun //calling private method will give error

######################################################################################
class Bank():
def __init__(self,uname):
self.__name = uname //內部attribute
self.__balance = 0 //內部attribute
def save(self, amt): //只能在method中操作到內部attribute
self.__balance += amt
print("存款:",amt,"完成")
def withdraw(self,amt):
self.__balance -= amt //只能在method中操作到內部attribute
print("提款:",amt,"完成")
def balance(self):
print("餘額:",self.__balance) //只能在method中操作到內部attribute

mybank = Bank('weili')
mybank.save(100)
mybank.withdraw(25)
mybank.balance()

Inheritance繼承

  • 有父類別(或稱基底類別Base Class)及子類別(Sub Class)的階層關係
  • 子類別會擁有父類別所有屬性(Attribute)及方法(Method)
# Creating a Base class
class Base:
# Declaring public method
def fun(self):
print("Public method")
# Declaring private method
def __fun(self):
print("Private method")

# Creating a derived class
class Derived(Base):
def __init__(self):
# Calling constructor of Base class
Base.__init__(self)

def call_public(self):
# Calling public method of base class
print("\nInside derived class")
self.fun()

def call_private(self):
# Calling private method of base class
self.__fun()

Polymorphism多型

  • 能夠讓程式碼的相依性不會那麼高,並且透過統一的介面來彈性擴充功能
  • 當子類別與父類別有同一個名稱的method時,子類別會父寫父類別的method
class Car:
def __init__(self, name, color, price):
self.name = name
self.color = color
self.price = price
def show(self):
print('Details:', self.name, self.color, self.price)
def max_speed(self):
print('Car max speed is 150')
def change_gear(self):
print('Car change 6 gear')

# inherit from Car class
class SportsCar(Car):
def max_speed(self): //child can rewrite method
print('Sports Car max speed is 240')
def change_gear(self): //child can rewrite method
print('Sports Car change 7 gear')

c1 = SportsCar('Audi999', 'white', 75000)
c1.show()
c1.max_speed()
c1.change_gear()