我想将 A 类型的对象转换为 B 类型,这样我就可以使用 B 的方法。B类继承A。例如我有我的类B:
class B(A):
def hello(self):
print('Hello, I am an object of type B')
我的库 Foo 有一个函数,它返回一个 A 类型的对象,我想将它转换为 B 类型。
>>>import Foo
>>>a_thing = Foo.getAThing()
>>>type(a_thing)
A
>>># Somehow cast a_thing to type B
>>>a_thing.hello()
Hello, I am an object of type B