python - 实际上,Python中带有参数的多重继承在没有指针的情况下不起作用
问题描述
class First:
def __init__(self, x):
self.x = x
print("first")
def __str__(self):
return " " + str(self.x)
class Second:
def __init__(self, y):
self.y = y
print("second")
def __str__(self):
return " " + str(self.y)
class Third(First, Second):
def __init__(self, x, y, z):
Second(y)
First(x)
self.z = z
print("third")
def __str__(self):
return Second.__str__(self) + " " + str(self.z)
o = Third(12, 6, 5)
解决方案
我认为你想要的是:
class Third(First, Second):
def __init__(self,x,y,z):
Second.__init__(self, y) # initializes Second subclass
First.__init__(self, x) # initializes First subclass
self.z=z
print("third")
def __str__(self):
return Second.__str__(self)+" "+ str(self.z)
现在你可以这样做:
>>> o = Third(12,6,5)
second
first
third
>>> print(o)
6 5
>>> print(o.x, o.y, o.z)
12 6 5
推荐阅读
- node.js - 我正在用 index.js 和 .env 编写 Discord bot,在终端“node index.js”中运行它时遇到了这个错误
- java - PowerMock 不会模拟静态方法以在 Spring-Boot 应用程序中引发异常
- java - 如何在 Android recyclerview 中阻止搜索过滤器不断崩溃
- python - Python:psycopg2.OperationalError:致命:连接限制
- python - (Python) 如何获取执行文件操作的用户的名称?
- c# - 动态注册 DLL 后如何在 Unity 中使用 DI
- c# - GetAwaiter().GetResult() 和 .Result 都不适合我,但 Task.Run(await ()=> nameOfMethodTobeCalled()).Result 工作。我没看懂?
- reactjs - Next.js 具有 React 钩子和 localStorage 的持久状态
- c++ - 返回值与写出的值不同
- c# - 可以像在实体框架中一样在 T-SQL 中将表声明为对象吗?