如何从另一个类(java)访问主类中的变量? [英] How do I access variables from the main class from another class (java)?

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

问题描述

我正在用 Java 制作一个 cookie clicker 克隆来练习我的 Java 技能,但我有一个小问题,我有一些在 main 方法中声明的变量,我想从 ActionListener 类访问这些变量.下面是来自 ActionListener 类的一些示例代码.int 变量(例如 clicks、grandamaCost)和 JTextFields(例如 display、cpsDisplay)都在 main 方法中.我想知道如何访问 main 方法中的变量,以便此代码可以在其他类中工作.谢谢!

I'm making a cookie clicker clone in java to practice my java skills and I have a small problem, I have variables that are declared in the main method that I want to access from an ActionListener class. Here is some sample code from the ActionListener class. the the int variables (ex. clicks, grandamaCost) and the JTextFields (ex. display, cpsDisplay) are all in the main method. I was wondering how I could have access to variables in the main method so that this code could work in the other class. Thanks!

@Override
public void actionPerformed(ActionEvent e) {
    JButton b = (JButton) e.getSource();
    button(b.getText());
}

public void button(String input) {
    switch (input) {
        case "Cookie":
            clicks++;
            display.setText("Cookies: " + clicks + "");
            cpsDisplay.setText("CPS: " + cps);
            break;
        case "Buy grandma":
            if (clicks >= grandmaCost) {
                grandmas++;
                clicks = clicks - grandmaCost;
                grandmaCost = (int) ((.15 * grandmaCost) + grandmaCost);
                cps++;
            }
            display.setText("Cookies: " + clicks + "");
            prices[0].setText("$" + grandmaCost);
            cpsDisplay.setText("CPS: " + cps);
            break;
        case "Buy monkey":
            if (clicks >= monkeyCost) {
                monkeys++;
                clicks = clicks - monkeyCost;
                monkeyCost = (int) ((.15 * monkeyCost) + monkeyCost);
                cps = cps + 2;
            }
            display.setText("Cookies: " + clicks + "");
            prices[1].setText("$" + monkeyCost);
            cpsDisplay.setText("CPS: " + cps);
            break;
        case "Buy Teemo":
            if (clicks >= teemoCost) {
                teemos++;
                clicks = clicks - teemoCost;
                teemoCost = (int) ((.15 * teemoCost) + teemoCost);
                cps = cps + 3;
            }
            display.setText("Cookies: " + clicks + "");
            prices[2].setText("$" + teemoCost);
            cpsDisplay.setText("CPS: " + cps);
            break;
    }
}

}

推荐答案

你的变量应该是 fields.

字段在类的方法之外声明,通常位于类声明的正下方.字段可以被类的所有方法访问.

Fields are declared outside of a class's methods and are usually found right below the class declaration. Fields can be accessed by all methods of a class.

也可以使用点运算符从其他类(除非它们是私有的)访问它们.

They can also be accessed from other classes (unless they are private) using the dot operator.

  • 如果字段被标记为static,则使用其类名来引用它.
  • 如果字段不是静态的,则使用其类的对象来引用它.
  • If a field is marked with static, its class name is used to reference it.
  • If a field is not static, an object of its class is used to reference it.

示例

    public class Man {
        public String name; //this is a field
        public static String gender = "Male"; //this is a static field

        public Man(String newName) {
            name = newName; //assigns the value of a field from within a method
        }
    }

和另一个班级...

public class Main {
    public static void main(String[] args) {
        Man bob = new Man("Bob");
        System.out.println(bob.name); //referenced from object, prints Bob
        System.out.println(Man.gender); //referenced from class name, prints Male
    }
}

为了更好地控制对字段的访问,您可以使用getter 和 setter.读一读!

And to have more control over the access of your fields, you can use getters and setters. Take a read!

这篇关于如何从另一个类(java)访问主类中的变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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