如何在Python中运行抽象方法(child将实现它)和抽象hirrachy
我对Python完全陌生,我想将Java项目转换为Python,这是我Java代码的一个基本示例:(我真的想知道如何使用Python中的抽象类和多态性) 这就是我在Python中尝试做的:如何在Python中运行抽象方法(child将实现它)和抽象hirrachy,python,polymorphism,abstract-class,Python,Polymorphism,Abstract Class,我对Python完全陌生,我想将Java项目转换为Python,这是我Java代码的一个基本示例:(我真的想知道如何使用Python中的抽象类和多态性) 这就是我在Python中尝试做的: import abc from abc import ABCMeta, abstractmethod class AbstractGrandFather(object): __metaclass__ = ABCMeta @abc.abstractmethod def __init_
import abc
from abc import ABCMeta, abstractmethod
class AbstractGrandFather(object):
__metaclass__ = ABCMeta
@abc.abstractmethod
def __init__(self):
list = [self.get_command_name(self)]
@abc.abstractmethod
def get_command_name(self):
pass
@property
def get_list(self):
return self.list
class AbstractParent(AbstractGrandFather):
__metaclass__ = ABCMeta
@abc.abstractmethod
def __init__(self, name):
self.list = super.get_list.append(name)
@abc.abstractmethod
def get_command_name(self):
pass
class Child(AbstractParent):
def get_command_name(self):
return "Samuel"
def __init__(self, father_name, child_name):
self.list = super(father_name).get_list.append(child_name)
x = Child("Dan," "Ben")
但它不起作用,我得到一个错误:
Traceback (most recent call last):
File "Dummy.py", line 43, in <module>
x = Child("Dan," "Ben")
TypeError: __init__() takes exactly 3 arguments (2 given)
回溯(最近一次呼叫最后一次):
文件“Dummy.py”,第43行,在
x=孩子(“丹”、“本”)
TypeError:\uuuu init\uuuuuu()正好接受3个参数(给定2个)
我走对了吗?我将非常感谢您的帮助和指导
谢谢编辑:修复了multipython兼容性 编辑2:在结尾添加了一些指针 看看这个:
class AbstractGrandFather(object):
__metaclass__ = ABCMeta
@abc.abstractmethod
def __init__(self):
self._list = [self.get_command_name()]
@abc.abstractmethod
def get_command_name(self):
pass
@property
def list(self):
return self._list
class AbstractParent(AbstractGrandFather):
__metaclass__ = ABCMeta
@abc.abstractmethod
def __init__(self, name):
super(AbstractParent, self).__init__()
self.list.append(name)
@abc.abstractmethod
def get_command_name(self):
pass
class Child(AbstractParent):
def get_command_name(self):
return "Samuel"
def __init__(self, father_name, child_name):
super(Child, self).__init__(father_name)
self.list.append(child_name)
在Python3上,它的super()
就足够了,不需要super(类、实例)
您可能想先了解一下
super
,super
在Python中的工作方式并不完全相同(因为Python支持多重继承)。此外,我们还需要知道您所针对的Python版本,因为Python3在super
中引入了一些更改。第三哦,我的。。。Python中的getter。shudderIm在PyCharm上工作,解释器是Python2.7.2,getter只是因为我不知道如何进入“祖父”字段“列表”,否则(用于附加)会很高兴找到更好的解决方案。修复您的行:x=Child(“Dan”,“Ben”)
它仍然给我:回溯(最近一次调用):文件“Dummy.py”,第43行,在x=Child(“Dan”,“Ben”)TypeError:uuu init_uuuu()正好接受3个参数(给定2个),我正在处理PyCharm,解释器是Python 2.7.2,对我来说也是如此,Python 2.7.5和Python 3.3.2这是我的错,需要更改:x=Child(“Dan”,“Ben”)你是最好的!你这方面有点不走运。Python“…”
的工作原理与“…+”
的工作原理相同,因此您得到的是一个完全无关的错误。
class AbstractGrandFather(object):
__metaclass__ = ABCMeta
@abc.abstractmethod
def __init__(self):
self._list = [self.get_command_name()]
@abc.abstractmethod
def get_command_name(self):
pass
@property
def list(self):
return self._list
class AbstractParent(AbstractGrandFather):
__metaclass__ = ABCMeta
@abc.abstractmethod
def __init__(self, name):
super(AbstractParent, self).__init__()
self.list.append(name)
@abc.abstractmethod
def get_command_name(self):
pass
class Child(AbstractParent):
def get_command_name(self):
return "Samuel"
def __init__(self, father_name, child_name):
super(Child, self).__init__(father_name)
self.list.append(child_name)