从Java角度到Python的基本对象实例化
我正试图通过将我不久前做的一个旧Java项目移植到Python来学习Python 但我会用一种更简单的方式来表达我的怀疑 我想使我的代码尽可能面向对象 这是我最基本的怀疑从Java角度到Python的基本对象实例化,java,python,oop,porting,Java,Python,Oop,Porting,我正试图通过将我不久前做的一个旧Java项目移植到Python来学习Python 但我会用一种更简单的方式来表达我的怀疑 我想使我的代码尽可能面向对象 这是我最基本的怀疑 public class MainApp { public static void main(String[] args) { AnotherClass another = new AnotherClass(); // I instantiated an object System.exit(0); } }
public class MainApp {
public static void main(String[] args) {
AnotherClass another = new AnotherClass(); // I instantiated an object
System.exit(0);
}
}
当我实例化这个对象时,我可以在另一个类()中处理过程代码并从中获得输出
public class AnotherClass {
public AnotherClass(){
System.out.println("Output from AnotherClass()");
}
}
我明白了:
Output from AnotherClass()
我如何做同样的事情,但在Python中。。。(这是我第一次尝试用Python理解OOP!)
我希望对我的问题的回答能帮助另一个希望通过移植学习新语言的程序员
编辑:我目前正在使用python 2.7。。。。
很抱歉没有声明版本…以下是如何将另一个类转换为python
class AnotherClass:
def __init__(self):
print("Output from AnotherClass()")
self._x = 0
another_class = AnotherClass() # get instance of AnotherClass
python中没有新的。我假设您已经知道Java中的
this
关键字,python中的self
做同样的事情,您可以访问像self这样的实例变量。\u x
但是self
不是保留关键字
class AnotherClass:
def __init__(self):
print("Output from AnotherClass()")
self._x = 0
another_class = AnotherClass() # get instance of AnotherClass
python中没有新的。我假设您已经知道Java中的
this
关键字,python中的self
做同样的事情,您可以访问像self这样的实例变量。x
但是self
不是保留关键字您是否只需要print()
函数(或者python 2.x版本中的print
语句)要输出字符串?您应该阅读教程:我正在使用Python 2.7:)然后教程是您是否只想使用print()
函数(或Python 2.x版本中的print
语句)输出字符串?您应该阅读教程:我正在使用Python 2.7:)然后教程是