2024-04-20 03:03:41 发布
网友
考虑以下代码:
class A: def hi(self): print 'A' def change(self): self = B() class B(A): def hi(self): print 'B'
以及
是否有某种方法可以使这个原则起作用,从而从类/对象本身改变对象引用“test”?在
对象没有包含它们的变量的概念-因此,您不能完全执行您要做的事情。在
你可以做的是有一个容器,它知道它包含的东西:
class Container(object): def __init__(self): self.thing = A() def change(self): self.thing = B() def hi(self): self.thing.hi() test = Container() test.hi() # prints A test.change() test.hi() # prints B
对象没有包含它们的变量的概念-因此,您不能完全执行您要做的事情。在
你可以做的是有一个容器,它知道它包含的东西:
相关问题 更多 >
编程相关推荐