interface classes and abstract classes for Python programming design patterns

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.