如何将对象从不同的 class 访问到 Python 中的一个 class?

How can you access an object from a different class into one class in Python?

如何从另一个 class 访问一个对象到另一个 class?

class Hello:
     def say_hello (self):
          print("Hello!")

class Robot:
     def hello (self):
          a = Hello()
          a.say_hello()

例如,在下面的代码中,我有一个"Hello"和"Robot"class。我希望机器人通过使用 "Hello" class 中的 "say_hello" 方法来说 "hello"。在此特定代码中,它有效。 然而,对于更复杂的代码,同样的事情会奏效吗?

非常感谢您。

class Hello:
 def say_hello (self):
      print("Hello!")

class Robot:
 def hello (self):
      a = Hello()
      a.say_hello()
 y = Robot()
 y.hello()

照常工作。