Interface class
""" Interface Class is a programming design pattern, in Python there is no interface class borrowed Java thought to create a standard design pattern to support multiple inheritance, multi-dimensional specification " " "
Example:
fromAbcImportAbstractmethod, AbcmetaclassClergy (Metaclass=abcmeta):#The canonical subclass must have a cure method@abstractmethod#how to standardize the decoration defCure (Self, HP):#the standard method must pass, cannot realize PassclassHoly (clergy):defCure (Self, HP): HP+ = 100returnHPclassDiscipline (clergy):defCure (Self, HP): HP+ = 100returnHPclassShadow (clergy):defZhiliao (self, HP): HP+ = 100returnHP#create three branches of the priestHoly =Holy () discipline=discipline ()#shadow = Shadow () # Shadow class does not have a defined method cure, can not be instantiated, the instantiation will be an error alert#all have healing skills.Print(Holy.cure (10))# thePrint(Discipline.cure (20))# -
Abstract class
""" abstract class Python supports abstract classes, General Specification single inheritance " " "
Example
ImportABCclassClergy (METACLASS=ABC. Abcmeta):#The canonical subclass must have a cure method@abc. AbstractmethoddefCure (Self, HP):#The canonical method can have a certain code functionHP + = 100returnHPclassHoly (clergy):defCure (Self, HP): HP+ = 100returnHPclassDiscipline (clergy):defCure (Self, HP): HP+ = 100returnHPclassShadow (clergy):defZhiliao (self, HP): HP+ = 100returnHP#create three branches of the priestHoly =Holy () discipline=discipline ()#shadow = Shadow () # Shadow class does not have a defined method cure, can not be instantiated, the instantiation will be an error alert#all have healing skills.Print(Holy.cure (10))# thePrint(Discipline.cure (20))# -
interface classes and abstract classes for Python programming design patterns