python
設計模式
template
class Car:
def Drive(self):
pass
def Start(self):
pass
def Stop(self):
pass
def Run(self):
pass
class BMW(Car):
def Drive(self):
print('BMW is Driving')
def Start(self):
print('BMW is starting')
def Stop(self):
print('BMW is Stop')
def Run(self):
self.Start()
self.Drive()
self.Stop()
class Toyota(Car):
def Drive(self):
print('Toyota is Driving')
def Start(self):
print('Toyota is starting')
def Stop(self):
print('Toyota is Stop')
def Run(self):
self.Start()
self.Drive()
self.Stop()
B = BMW()
B.Run()
T =Toyota()
T.Run()
程式碼說明
定義car 類別作為interface,BMW繼承Car去複寫方法,Toyota繼承Car去複寫方法,最後執行比較結果。
留言
張貼留言