如何在类中创建方法,在另一个类中操作变量? [英] How to make a method in a class, manipulate variables in another class?

查看:157
本文介绍了如何在类中创建方法,在另一个类中操作变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Java进行简单的井字游戏。

Working on a simple tic-tac-toe game in Java.

我有一个名为的游戏GameHelpers 。该类应包含有用的游戏方法。游戏发生在另一个类中。

I have a class named GameHelpers. This class should contain useful methods for the game. The game happenes in another class.

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

A method in GameHelpers is ResetGame(). This method is supposed to set the text on all the 9 buttons (the tic-tac-toe board) to blank, set them enabled again, and set a variable to 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 [] 是游戏主类中的JButtons数组, TicTacToe

buttons[] is an array of JButtons inside the main class of the game, TicTacToe.

此方法以前位于游戏的主类中, TicTacToe 。但是现在它处于不同的类中,它无法到达 TicTacToe 类中的按钮并操纵它们。

This method was previously inside the main class of the game, TicTacToe. But now that it's in a different class, it can't reach the buttons in the TicTacToe class and manipulate them.

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

I created get and set methods in TicTacToe, but how do I activate them from GameHelpers?

如何在 GameHelpers 中使用该方法?

推荐答案

您可以将 Java引用到EXE - 为什么,何时,何时以及如何


缺点

磁盘占用空间。 Java字节码专为紧凑性而设计,因此它具有比典型CPU指令集高得多的水平。
预计AOT编译器生成的可执行文件将比原始jar文件大2-4%b $ b倍。

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.

动态应用程序即可。应用程序
开发人员可能无法使用应用程序在运行时动态加载的类。这些可以是第三方插件,动态代理和
运行时生成的其他类等等。因此运行时系统
必须包含一个Java字节码解释器和/或一个JIT编译器。

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.

特定于硬件的优化。 JIT编译器具有优于AOT编译器的潜在优势,因为它可以根据应用程序执行$ ​​b $ b的实际硬件选择代码生成
模式。例如,它可以使用英特尔MMX / SSE / SSE2扩展来进行
加速浮点计算。 AOT编译器必须要么
为最小公分母生成代码,要么将版本化应用到
最大的CPU密集型方法,这会导致代码大小增加

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.

这篇关于如何在类中创建方法,在另一个类中操作变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆