Template Method Mode: Defines the skeleton of an algorithm in an operation, and delays some steps to the subclass. Therefore, the template method allows subclass to redefine certain variables of an algorithm without changing the structure of an algorithm.
Advantage: Moving the unchanged behavior to the superclass removes repeated code in the subclass.
# Encoding = UTF-8 ## by Panda # template method mode def printinfo (Info): Print Unicode (Info, 'utf-8 '). encode ('gbk') # class testpaper: def testquestion1 (Self): printinfo. ductile Iron B. tinplate C. high-speed alloy steel D. carbon fiber ') printinfo ('answer:' + self. answer1 () def testquestion2 (Self): printinfo. make this plant no longer harm B. extinction of a rare species C. damage the ecological balance D. cause desertification in the region ') printinfo ('answer:' + self. answer2 () def testquestion3 (Self): printinfo. aspirin B. niuhuang Jiedu tablets C. d. drinking a lot of raw milk E. none of the above) printinfo ('answer: '+ self. answer3 () def answer1 (Self): Return ''def answer2 (Self): Return ''def answer3 (Self): Return ''# student a class testpapera (testpaper ): def answer1 (Self): Return 'A' def answer2 (Self): Return 'B' def answer3 (Self): Return 'C' # student B class testpaperb (testpaper ): def answer1 (Self): Return 'B' def answer2 (Self): Return 'D' def answer3 (Self): Return 'E' def clientui (): A = testpapera ();. testquestion1 (). testquestion2 (). testquestion3 () B = testpaperb (); B. testquestion1 () B. testquestion2 () B. testquestion3 () returnif _ name _ = '_ main _': clientui ();
Class diagram: