Home > Backend Development > Python Tutorial > A brief discussion of the definition of interfaces, abstract classes and abstract methods in Python

A brief discussion of the definition of interfaces, abstract classes and abstract methods in Python

高洛峰
Release: 2016-11-19 09:27:46
Original
1496 people have browsed it

#_*_ coding:utf-8 _*_
#知识点:接口的定义和抽象类以及抽象方法
 
'''
1、抽象类式啥?
抽象类加上抽象方法就等于接口
 
2、接口的定义
含义1、别人给你暴露一个URL,然后调用这个URL
含义2、定义一个规范,不写具体实现,按照这个规范去实现相关功能,抽象类就是属于这种
'''
 
from abc import  ABCMeta, abstractmethod
 
#定义一个抽象类
class Alert:
    __metaclass__ = ABCMeta
    @abstractmethod
    def Send(self):pass #抽象方法
 
class Foo(Alert): #继承抽象类(或者说继承接口),就得按照抽象类的规范
    def __init__(self):
        print '__init__'
 
    def Send(self): #抽象类里有send方法,所以,在这里也必须要有send方法
        print 'send.Weixin'
 
f = Foo()
f.Send()
Copy after login


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template