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

标签 java class methods

正在使用 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/

相关文章:

c# - 从 Collection 类中提取数据

java - 如何调用另一个类中的List类型方法

javascript - 如何从 addEventListener() 调用中调用对象方法?

javascript 替换 + classid

java - 在主方法中调用类的构造函数?

java - 为什么 String.getBytes() 在编译后的 jar 中表现不同?

java - 通过 ajax 将 blob 发送到 servlet

ruby-on-rails - 模型中未定义方法?

java - 用 Java 开发的游戏中的渲染问题

java - 如何为 jar 文件设置最大 jvm 内存 (XMX)