|
模板方法模式:先定义一个基础的算法类,在子类中添加操作的步骤。
代码:
- import pyb
- class BaseLED:
- def SetTime1(self):
- pyb.LED(1).on()
- pyb.delay(self.Set1())
- pyb.LED(1).off()
- pyb.delay(self.Set1())
- def SetTime2(self):
- pyb.LED(2).on()
- pyb.delay(self.Set2())
- pyb.LED(2).off()
- pyb.delay(self.Set1())
- def Set1(self):
- return 100
- def Set2(self):
- return 1000
-
- class LED1(BaseLED):
- def Set1(self):
- return 50
- def Set2(self):
- return 150
-
- class LED2(BaseLED):
- def Set1(self):
- return 1500
- def Set1(self):
- return 200
-
- if __name__ == "__main__":
- l1 = LED1()
- l2 = LED2()
-
- l1.SetTime1()
- l1.SetTime2()
-
- l2.SetTime1()
- l2.SetTime2()
复制代码
将常用的操作方法归类到父类中,子类只提供不同的参数,完成指定的功能。
类似考卷,每个考卷的内容相同,考生填写自己的名字,即成为每个考生自己的考卷。
|
|