Python interface
In Python, there are two types of interfaces, one is the API interface that is accessed through a URL.
One is an interface to an object
Constructing interfaces
classIjiekou:"""Defining a constraint interface""" defF1 (Self,nid):RaiseException ('All classes inheriting this interface class must implement (present) the F1 method')classoduixiang1 (Ijiekou):"""The F1 method is implemented by the inheriting interface.""" defF1 (Self, nid):Print(NID)classoduixiang2 (Ijiekou):"""The inherited interface does not implement the F1 method"""#The F1 method is implemented by the inheriting interface.Obj1 =oduixiang1 () obj1.f1 (1)#The inherited interface does not implement the F1 methodObj2 =Oduixiang2 (abstract method abstract Class) Obj2.f1 (2) Obj2.f1 (2)
File "h:/shipbfq/22.py", line ten, in F1
Raise Exception (' All classes inheriting this interface class must implement (present) The F1 method ')
Exception: All classes inheriting this interface class must implement (present) the F1 method
Abstract method Abstract Class
The difference between abstract class and ordinary class is that abstract methods can be defined in abstract classes, and abstract methods can be used as constraints, which are inherited abstract classes, must implement (exist) abstract methods, or error
Defining an abstract class must introduce an ABC module
ImportABCclassIjiekou (metaclass=ABC. Abcmeta):"""The definition abstract class must be set to METACLASS=ABC. Abcmeta""" defF1 (Self,nid):"""Common Methods""" Print(123) @abc. AbstractmethoddefF2 (self):"""abstract method, set abstract method must add adorner @abc.abstractmethod"""classoduixiang1 (Ijiekou):"""Inheriting abstract Classes""" defF3 (Self, nid):Print(NID)#def f2 (Self,nid): # """ #inherited abstract classes, and abstract classes have abstract methods, you must implement an abstract method or error, (constraint) # """ #print (456)#The F1 method is implemented by the inheriting interface.Obj1 =oduixiang1 () obj1.f1 (1)
Error
E:\Evns\jxiou\Scripts\python.exe h:/shipbfq/22.py
Traceback (most recent):
File "h:/shipbfq/22.py", line Notoginseng, in <module>
Obj1 = Oduixiang1 ()
Typeerror:can ' t instantiate abstract class oduixiang1 with abstract methods F2
Peddling a recording network
Recording website
Section No. 412, Python interface, abstract method abstract class