本文介绍了如何将(继承)父类转换为子类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想知道如何将由某个函数返回的父对象转换为子类。
对象):
def __init __():
pass
class B(A):
def functionIneed():
pass
b $ bi = module.getObject()#i将获取类A的对象
j = B(i)#这将返回异常
j.functionIneed()
我不能更改类A.如果我可以实现functionIneed到类A,但是因为代码的结构是不可能的。
感谢
解决方案
Python不支持转换。您将需要写 B .__ init __()
,以便它可以接受 A
并适当地初始化。 p>
I would like to know how to convert parent object that was return by some function to child class.
class A(object):
def __init__():
pass
class B(A):
def functionIneed():
pass
i = module.getObject()# i will get object that is class A
j = B(i)# this will return exception
j.functionIneed()
I cannot change class A. If I could I would implement functionIneed to class A, but it is impossible because of structure of code.Thanks
解决方案
Python does not support "casting". You will need to write B.__init__()
so that it can take a A
and initialize itself appropriately.
这篇关于如何将(继承)父类转换为子类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!