【问题标题】:How can i write this Java code in Python?我如何用 Python 编写这个 Java 代码?
【发布时间】:2011-11-24 11:53:54
【问题描述】:

如何用 Python 编写这个(Java)?

class Test
{
  private String test1 = "test";
  private static void main(String args[])
  {
    System.out.println("test" + test1);
  }
}

【问题讨论】:

  • 你不能在 python 中拥有私有的东西。最接近它的是name mangling
  • 实际上您的示例甚至无法编译,因为test1 是一个实例变量,而main 是一个静态函数。

标签: python python-3.x


【解决方案1】:

由于 Python 支持类之外的代码,因此等价物很简单:

test1 = "test"
print("test" + test1)

更新:获取参数:

import sys
print sys.argv

你真的应该从the tutorial开始!

【讨论】:

  • 但是我如何在你的例子中得到 args[] ?就像我的一样,我可以 $ java -jar Test.jar "command line input"
  • 你的例子没有使用 args。
【解决方案2】:

嗯,像这样:

test1 = "test"
print "test" + test1

但是如果你使用的是 Python 3,你必须写 print("test" + test1)

【讨论】:

    猜你喜欢
    • 2020-09-07
    • 1970-01-01
    • 2023-04-03
    • 1970-01-01
    • 2015-03-08
    • 2019-08-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多