Section No. 412, Python interface, abstract method abstract class

Source: Internet
Author: User

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

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.