gpt4 book ai didi

java - 如何在一个类中做一个方法,在另一个类中操作变量?

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:15:54 25 4
gpt4 key购买 nike

正在使用 Java 开发一个简单的井字棋游戏。

我有一个名为 GameHelpers 的类。这个类应该包含对游戏有用的方法。游戏发生在另一个类(class)。

GameHelpers 中的一个方法是ResetGame()。此方法应该将所有 9 个按钮(井字游戏板)上的文本设置为空白,再次将它们设置为启用,并将变量设置为 1。

这是它的代码:

public class GameHelpers {

public void resetGame(){
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
buttons[i][j].setEnabled(true);
buttons[i][j].setText("");
count = 1;
}
}
}

}

buttons[] 是游戏主类 TicTacToe 中的 JButton 数组。

此方法之前位于游戏的主类 TicTacToe 中。但现在它在不同的类中,它无法访问 TicTacToe 类中的按钮并对其进行操作。

我在 TicTacToe 中创建了 get 和 set 方法,但是如何从 GameHelpers 激活它们?

如何使 GameHelpers 中的方法起作用?

最佳答案

您可以引用Java to EXE - Why, When, When Not and How

Drawbacks

Disk footprint. Java bytecode has been designed for compactness, so it has a much higher level than a typical CPU instruction set. Expect that an executable produced by an AOT compiler will be 2-4 times larger than the original jar file.

Dynamic applications. Classes that the application loads dynamically at runtime may be unavailable to the application developer. These can be third-party plug-ins, dynamic proxies and other classes generated at runtime and so on. So the runtime system has to include a Java bytecode interpreter and/or a JIT compiler.

Moreover, in the general case only classes that are loaded by either system or application classloader may be precompiled to native code. So applications that use custom classloaders extensively may only be partially precompiled.

Hardware-specific optimizations. A JIT compiler has a potential advantage over AOT compilers in that it can select code generation patterns according to the actual hardware on which the application is executing. For instance, it may use Intel MMX/SSE/SSE2 extensions to speedup floating point calculations. An AOT compiler must either produce code for the lowest common denominator or apply versioning to the most CPU-intensive methods, which results in further code size increase.

关于java - 如何在一个类中做一个方法,在另一个类中操作变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20638708/

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