设计模式注意事项¶
继承
的问题在于:改变超类,将影响众多子类,很难发现子类的全部方法。- 不论当年设计多好,一段时间后总要成长和改变,不然就会 死亡。
- 一: 区分变 和 不变 , 进行分离。 也就是让系统中某部分改变,不影响其他部分!!!
- 二:面向接口编程,而不是面向实现编程。
code examples¶
class Animal():
def __init__(self, name):
self.name = name
def make_sound():
print("make sound")
class Dog(Animal):
def make_sound(self):
self.bark()
def bark(self):
print("wangwang...")
class Cat(Animal):
def make_sound(self):
self.meow()
def meow(self):
print("momo...")
d = Dog("six")
d.make_sound()