简单的用策略模式熟悉Python编程
# -*- coding: utf-8 -*-
"""
Created on Mon Feb 12 15:48:29 2018
@author: mz
"""
class Strategy(object):
def action(self):
pass
class ConcreteStrategyA(Strategy):
def action(self):
print("A action")
class ConcreteStrategyB(Strategy):
def action(self):
print("B action")
class Context(object):
def __init__(self, rhs):
self.strategy = rhs
def action(self):
self.strategy.action()
#print(self.strategy.action()) --输出: None
if "__main__" == __name__:
context = Context(ConcreteStrategyA())
context.action()
输出结果:
A action
顺带了解了下print函数,如果函数action无返回值,则打印空对象None