从 Java 到 Python 的基本对象实例化

Basic object instantiation from a Java point of view to Python

我正在尝试通过将我前一段时间做的旧 Java 项目移植到 Python 来学习 Python。

但我会在这里以更简单的方式提出我的疑问...

我想让我的代码尽可能面向对象...

这是我得到的基本疑惑...

public class MainApp {
public static void main(String[] args) {
    AnotherClass another = new AnotherClass(); // I instantiated an object
    System.exit(0);
  }
}

当我实例化此对象时,我可以在 AnotherClass() 中处理过程代码并从那里获取输出...

public class AnotherClass {
   public AnotherClass(){
    System.out.println("Output from AnotherClass()");
   }
}

我明白了:

Output from AnotherClass()

我怎样才能做同样的事情,但在 Python...(这是我第一次尝试理解 OOP,但在 Python!)

我希望我的问题的答案能帮助到另一个想通过移植学习一门新语言的程序员!

编辑:我目前正在使用 python 2.7.... 很抱歉没有声明版本...

下面是将 AnotherClass 翻译成 python

的方法
class AnotherClass:
    def __init__(self):
        print("Output from AnotherClass()")
        self._x = 0


another_class = AnotherClass()  # get instance of AnotherClass 

python 中没有 new。我假设您已经知道 Java 中的 this 关键字,python 中的 self 做同样的事情,您可以访问实例变量,例如 self._x 但是 self 不是保留关键字