Python 简明教程
Python - Interfaces
在软件工程中, interface 是一种软件架构模式。它类似于一个类,但它的方法只有原型签名定义,没有任何可执行代码或实现体。必需的功能必须通过继承接口的任何类的某些方法来实现。
In software engineering, an interface is a software architectural pattern. It is similar to a class but its methods just have prototype signature definition without any executable code or implementation body. The required functionality must be implemented by the methods of any class that inherits the interface.
Interfaces in Python
在 Java 和 Go 等语言中,都有一个称为 interface 的关键字,用于定义接口。Python 没有它或任何类似的关键字。它使用抽象基类(简称 ABC 模块)和 @abstractmethod 装饰器来创建接口。
In languages like Java and Go, there is keyword called interface which is used to define an interface. Python doesn’t have it or any similar keyword. It uses abstract base classes (in short ABC module) and @abstractmethod decorator to create interfaces.
NOTE: 在 Python 中,抽象类也是使用 ABC 模块创建的。
NOTE: In Python, abstract classes are also created using ABC module.
在 Python 中,抽象类和接口看起来类似。两者的唯一区别是抽象类可能有一些非抽象方法,而接口中的所有方法都必须是抽象的,并且实现类必须重写所有抽象方法。
An abstract class and interface appear similar in Python. The only difference in two is that the abstract class may have some non-abstract methods, while all methods in interface must be abstract, and the implementing class must override all the abstract methods.
Rules for implementing Python Interfaces
在 Python 中创建和实现接口时,我们需要考虑以下几点 −
We need to consider the following points while creating and implementing interfaces in Python −
-
Methods defined inside an interface must be abstract.
-
Creating object of an interface is not allowed.
-
A class implementing an interface needs to define all the methods of that interface.
-
In case, a class is not implementing all the methods defined inside the interface, the class must be declared abstract.
Ways to implement Interfaces in Python
我们可以通过两种方式创建和实现接口 −
We can create and implement interfaces in two ways −
-
Formal Interface
-
Informal Interface
Formal Interface
Python 中的正式接口是使用抽象基类 (ABC) 实现的。要使用这个类,你需要从 abc 模块中导入它。
Formal interfaces in Python are implemented using abstract base class (ABC). To use this class, you need to import it from the abc module.
Example
在这个示例中,我们使用两种抽象方法创建了一个正式接口。
In this example, we are creating a formal interface with two abstract methods.
from abc import ABC, abstractmethod
# creating interface
class demoInterface(ABC):
@abstractmethod
def method1(self):
print ("Abstract method1")
return
@abstractmethod
def method2(self):
print ("Abstract method1")
return
让我们提供一个类实现两个抽象方法。
Let us provide a class that implements both the abstract methods.
# class implementing the above interface
class concreteclass(demoInterface):
def method1(self):
print ("This is method1")
return
def method2(self):
print ("This is method2")
return
# creating instance
obj = concreteclass()
# method call
obj.method1()
obj.method2()
Informal Interface
在 Python 中,非正式接口表示的是一个类,其中包含可以被覆盖的方法。然而,编译器无法严格地强制实现所有提供的方法。
In Python, the informal interface refers to a class with methods that can be overridden. However, the compiler cannot strictly enforce the implementation of all the provided methods.
这种类型的接口基于鸭子类型化原理工作。只要方法存在,它就可以使我们调用对象上的任何方法,而不用检查其类型。
This type of interface works on the principle of duck typing. It allows us to call any method on an object without checking its type, as long as the method exists.
Example
在下面的示例中,我们演示了非正式接口的概念。
In the below example, we are demonstrating the concept of informal interface.
class demoInterface:
def displayMsg(self):
pass
class newClass(demoInterface):
def displayMsg(self):
print ("This is my message")
# creating instance
obj = newClass()
# method call
obj.displayMsg()