简体   繁体   English

如何从Java中的不同类访问变量?

[英]How do you access variables from different classes in Java?

I have been having trouble accessing a variable from another class. 我一直无法从另一个类访问变量。 Here are my variables: 这是我的变量:

public static byte agressivePoints = 0;
public static byte peacefullPoints = 0;
public static byte meanPoints = 0;
public static byte happyPoints = 0;
public static byte sadPoints = 0;

I put them outside of my Main class. 我把它们放在我的主课堂之外。 In this class, I am tying to access those variables from the other class to put them in this one. 在这个类中,我想从其他类中访问这些变量以将它们放在这个类中。

public class Check_Anwser {
    public static void Check_Answer(int Input) {
        boolean TryAgain = false;

        do {
            switch (Input) {
                case 'a': {
                    ++agressivePoints;
                    break;
                }
                case 'b': {
                    ++sadPoints;
                    break;
                }
                case 'c': {
                    ++meanPoints;
                    break;
                }
                case 'd': {
                    ++peacefullPoints;
                    break;
                }
                case 'e': {
                    ++happyPoints;
                }
                default: {
                    System.out.println("You have entered an invalid anwser. Please try      again");
                }
            }
        } while (TryAgain != true);
    }
}
}

But when I do this, there is an error. 但是,当我这样做时,有一个错误。 For some reason, the variables from the first class aren't shared with the second class. 出于某种原因,第一类中的变量不与第二类共享。

How do you access variables from different classes in Java? 如何从Java中的不同类访问变量?

If the variable (or member) of another class is non-static. 如果另一个类的变量(或成员)是非静态的。 You can create an instance(object) of that class and access to its variable, for example: 您可以创建该类的实例(对象)并访问其变量,例如:

class Warrior
{
    private int strength;
    public int getStrength(){
        return this.strength;
    }
}

class TestRunner{
    public static void main(String[] args){
        Warrior w = new Warrior();
        w.getStrength();    //Access to strength of warrior via a Warrior object
    }     
}

If the member is public, you will be able to access it directly like: 如果该成员是公开的,您将能够直接访问它,如:

w.strength;

But when you learn the concept of encapsulation and data protection, you will realize generally we will try to make the instance variables as private. 但是当你学习封装和数据保护的概念时,你会发现我们通常会尝试将实例变量设为私有。


As for static members, they belongs to the class and not individual objects. 对于静态成员,它们属于类而不是单个对象。 So it is encouraged you access them via the class name, for example: 因此鼓励您通过类名访问它们,例如:

class Warrior{
    public static final int SPEED;    //let say speed for all warriors are unified
}

class TestRunner{
    public static void main(String[] args){
        int speed = Warrior.SPEED;    //Access a class variable via the class name
    }     
}

Use the expression Main.aggressivePoints to access these global variables. 使用表达式Main.aggressivePoints来访问这些全局变量。

I encourage you to keep learning about object-oriented design, so that you can understand why it will be better when you create objects and protect data like this inside them. 我鼓励您继续学习面向对象的设计,这样您就可以理解为什么在创建对象并保护其中的数据时会更好。 It's called encapsulation. 它被称为封装。

To access variables from other class say A to call the variable into your main class use (instance of classA).varaibleName In your case corresponding class name of the variable say class A contains 要从其他类访问变量,请说A将变量调用到主类中使用(classA的实例).varaibleName在您的情况下,对应的变量类名称说类A包含

public static byte agressivePoints = 0;

by creating an instance of the class like 通过创建类的实例

A inst =new A();
++inst.agressivePoints;

The best way to do this is have a Variables class, in which holds all your variables, and inside the Correct_Answer class you could simply call 执行此操作的最佳方法是使用Variables类,其中包含所有变量,而在Correct_Answer类中,您可以简单地调用

Static Variables v;

and infront of each of the variables you wish to call just do c.VARIABLENAME; 你希望调用的每个变量都面向c.VARIABLENAME;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM