TypeError: abstract class
时间: 2023-12-19 21:48:37 浏览: 81
PyQt5之QGraphics 011 QGraphicsItem之绘画OpenCV contours
An abstract class is a type of class in object-oriented programming that cannot be instantiated directly. It is designed to be subclassed and its methods overridden by its subclasses.
A `TypeError` with the message "abstract class" typically means that you are trying to instantiate an abstract class directly, which is not allowed. You need to create a concrete subclass of the abstract class and instantiate that instead.
Here's an example of an abstract class in Python:
```python
from abc import ABC, abstractmethod
class Animal(ABC):
@abstractmethod
def make_sound(self):
pass
```
This class defines an abstract method `make_sound()` which must be implemented by any subclass of `Animal`. If you try to create an instance of `Animal`, you will get a `TypeError`:
```python
>>> a = Animal()
TypeError: Can't instantiate abstract class Animal with abstract methods make_sound
```
Instead, you need to create a concrete subclass of `Animal` that implements `make_sound()`:
```python
class Cat(Animal):
def make_sound(self):
print("Meow")
c = Cat()
c.make_sound() # prints "Meow"
```
阅读全文