gpt4 book ai didi

java - 如何在 Python 中对 Java 进行类似的公共(public)静态声明?

转载 作者:行者123 更新时间:2023-12-01 14:05:36 25 4
gpt4 key购买 nike

在我的Java中,我有一个中央引导加载程序,它将所有默认值保留为公共(public)静态或私有(private)静态,稍后当需要时我可以转到其他类/线程并访问它们以进行修改等。例如:

public class main extends JWindow implements MouseListener, MouseMotionListener {

private static boolean isDetect = true;
public static String vncMode = "1980";
...

public main() {
vncMode = C.readIni("vncmode"); // 1980
}
}

public class TCPHandler implements Runnable {
import main.*;
public void run() {
if (main.vncMode.equals("1999" ) ||
main.vncMode.equals("2013")) {
echo(main.vncMode, RED);
} else {
echo(main.vncMode, GREEN);
}
}
}

与Java类似,在Python中,我如何设置公共(public)静态/私有(private)静态声明,以便我可以从该值的任何其他类访问?

python1.py:

from bgcolors import bgcolors
class Python1(object):
isDetect = True
def run(self):
# expecting vncMode = 1980
print bgcolors.RED + "we are now in 1999: from version: " + vncMode

python2.py:

from bgcolors import bgcolors
class Python2(object):
isDetect = True
def run(self):
# expecting vncMode = 1980
print bgcolors.RED + "we are now in 2013: from version: " + vncMode

main.py:

from bgcolors import bgcolors
from python1 import Python1
from python2 import Python2

vncMode = "1980"

a = Python1()
a.run()

b = Python2()
b.run()

我如何设置值 1980 并在所有类中获取 1980 ?

最佳答案

如果您有一个容器来存储您想要共享的属性

# example, could be a simple tuple, a full class or instance or dictionary, etc.
attributes = namedtuple("attributes", "vncmode")("1980")

您可以在初始化时将属性传递给所有类:

class ...:
def __init__(self, attributes, ...):
self.attributes = attributes

然后实例可以使用self.attributes.vncmode作为共享可变值。

关于java - 如何在 Python 中对 Java 进行类似的公共(public)静态声明?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18936926/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com