![](/img/trans.png)
[英]How to make an Abstract Class inherit from another Abstract Class in Python?
[英]How to inherit abstract class with PyCharm
我用Python IDLE编写了一个代码,其中包含一个抽象类和一个继承该抽象类*的类(两个类都位于同一文件夹中)。 效果很好。 我想将该代码复制并粘贴到PyCharm。 我制作了两张卡,一张带有抽象类,另一张带有继承该类的类,但出现此错误:
class Swords(Weapon):
TypeError: module.__init__() takes at most 2 arguments (3 given)
我很困惑,因为我没有更改代码。 这是代码
第一卡:武器
from abc import ABC, abstractmethod
class Weapon(ABC):
@abstractmethod
def __init__(self, name):
self.name = name
第二张牌:剑
import Weapon
class Swords(Weapon):
def __init__(self, name, attack_points, price):
super().__init__(name)
self.attack_points = attack_points
self.price = price
def info(self):
info = self.name + " is attack-weapon that increases attack points!"
return info
def __str__(self):
return "Sword name: {}\nSword attack: +{}\nSword price: {}\n".format(self.name,
self.attack_points,
self.price)
Elf_Sword = Swords("Elf Sword", 1, 50)
Fire_Sword = Swords("Fire Sword", 2, 80)
Space_Sword = Swords("Space Sword", 3, 120)
print(Elf_Sword)
请告诉我我做错了什么?
您可能在名为Weapon.py
的模块中拥有Weapon
类,而您只是在导入模块,而不是导入该类。
是否为ABC在这里并不重要。
作为一般的命名规则,
class Weapon:
pass
from weapon import Weapon
class Sword(Weapon):
pass
您不能导入类。 您必须导入模块或这些模块中的类。
在您的情况下,语法是
from file_where_weapon_is import Weapon
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.