设计一个抽象类,包含三个抽象方法,并添加两个子类继承
时间: 2023-08-18 10:25:51 浏览: 54
好的,以下是一个包含三个抽象方法的抽象类的设计:
```python
from abc import ABC, abstractmethod
class AbstractClass(ABC):
@abstractmethod
def method1(self):
pass
@abstractmethod
def method2(self):
pass
@abstractmethod
def method3(self):
pass
```
然后我们定义两个子类来继承这个抽象类:
```python
class ConcreteClass1(AbstractClass):
def method1(self):
print("ConcreteClass1 method1")
def method2(self):
print("ConcreteClass1 method2")
def method3(self):
print("ConcreteClass1 method3")
class ConcreteClass2(AbstractClass):
def method1(self):
print("ConcreteClass2 method1")
def method2(self):
print("ConcreteClass2 method2")
def method3(self):
print("ConcreteClass2 method3")
```
这两个子类实现了抽象类中的三个抽象方法。我们可以创建这两个子类的实例,并调用它们的方法:
```python
concrete1 = ConcreteClass1()
concrete1.method1()
concrete1.method2()
concrete1.method3()
concrete2 = ConcreteClass2()
concrete2.method1()
concrete2.method2()
concrete2.method3()
```
输出:
```
ConcreteClass1 method1
ConcreteClass1 method2
ConcreteClass1 method3
ConcreteClass2 method1
ConcreteClass2 method2
ConcreteClass2 method3
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)