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

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

问题描述

我在java中创建一个cookie clicker克隆练习我的java技能,我有一个小问题,我有变量,在main方法中声明,我想从一个ActionListener类访问。这里是一些来自ActionListener类的示例代码。 int变量(例如点击,grandamaCost)和JTextFields(例如display,cpsDisplay)都在main方法中。我想知道如何我可以访问在main方法中的变量,这样的代码可以在其他类中工作。谢谢!

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

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

}

您的变量应为字段 a>。



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



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




  • 如果一个字段标有 static ,它的类名用于引用它。

  •   public class Man {
    public String name; // this is a field
    public static String gender =Male; //这是一个静态字段

    public Man(String newName){
    name = newName; //指定方法内的字段值
    }
    }

    和另一个类...

      public class Main {
    public static void main(String [] args){
    Man bob = new Man(Bob);
    System.out.println(bob.name); //从对象引用,打印Bob
    System.out.println(Man.gender); //从类名引用,打印男
    }
    }

    可以更好地控制字段的访问权限,您可以使用 getters和setters 。阅读!


    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;
        }
    }
    

    }

    解决方案

    Your variables should be 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.

    • 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.

    Example

        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
            }
        }
    

    and another class...

    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
        }
    }
    

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

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

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