简体   繁体   English

Java:如何访问另一个类的变量

[英]Java: how do I access the variables of another class

I have a class GameScreen which has an instance of a class Sprites (I have named the instance gameSprites). 我有一个GameScreen类,它有一个Sprites类的实例(我将实例命名为gameSprites)。 In Sprites there is an instance of a third class, which I have named enemies. 在《精灵》中,有一个第三类的实例,我将其命名为敌人。

My question is, can I access a variable in enemies from GameScreen? 我的问题是,我可以从GameScreen访问敌人中的变量吗? Can I type gameSprites.enemies.variableName ? 我可以输入gameSprites.enemies.variableName吗? And can I continue that for more than two different classes? 我可以在两个以上的不同班级继续吗?

If Your Variable is Private you need to have Public method to access that Variable . 如果您的变量是私有的,则需要具有公共方法才能访问该变量。 Suppose I have This Dummy Enemies Class of Yours 假设我有这个假人

    package com;

public class Enemies {

    private final String name="HELLO";
    public final String names="This is public Variable";
    public static String name2="HELLO THIS IS STATIC";
    public Enemies(){};

    public String getName(){
        return name;
    }
}

and here in Sprites Class like your requirement i created Enemies instance 在这里像您的要求的Sprites类中,我创建了Enemies实例

package com;

public class Sprites {

    public Sprites(){};
    Enemies enemies = new Enemies();
}

This is Dummy GameScreen Class 这是虚拟的GameScreen类

package com;

    public class GameScreen {
        public static void main(String...strings){
        Sprites gameSprites = new Sprites();
        System.out.println(gameSprites.enemies.names);
        String name=gameSprites.enemies.name2;// This is Highly Discouraged Approach
        System.out.println(name);
        System.out.println(gameSprites.enemies.getName());
        }
    }

and the Output When You Run This Code. 和运行此代码时的输出。

This is public Variable
HELLO THIS IS STATIC
HELLO

So What you are trying to achieve can be done for public and Static variable(This one is not encouraged to do) . 因此,您可以尝试为public和Static变量实现目标(不鼓励这样做)。 For private you need to have a Public Method to access that Variable. 对于私有用户,您需要具有公共方法才能访问该变量。

In Java world, its not really recommended. 在Java世界中,不建议这样做。 You're able to access via objectName.memberName because your member attribute is a public one. 您可以通过objectName.memberName访问,因为您的member属性是公共属性。 As a convention, we usually keep members private and have getters and setter through which we access the values. 按照惯例,我们通常将成员保持私有状态,并具有用于获取值的获取器和设置器。

And coming back your question, you can access that way to any level given that each of the members are declared public. 回到您的问题,假设每个成员都已宣布为公开,则可以以任何方式访问该级别。 Say, gameSprites.enemies.enemy1.enemy1Army.solder1 is also possible if enemies is public in gameSprites class, enemy1 is public in enemies class and so on. 假设如果gameSprites类中的敌人是公开的,而敌人1类在敌人类中是公开的,gameSprites.enemies.enemy1.enemy1Army.solder1也是可能的。

This is really very basic things of any OOP language. 对于任何OOP语言,这实际上都是非常基本的事情。 In my opinion, you should spend some times on study those basic things, otherwise there is a good chance that you will stuck further when you proceed from this problem. 我认为,您应该花一些时间研究这些基本知识,否则很有可能在解决此问题时会进一步陷入困境。 However, here is an example to how to access variable of other class from another class. 但是,这是一个如何从另一个类访问另一个类的变量的示例。 Accessing a variable from another class 从另一个类访问变量

It might be accessible if there is public property of class "enemies". 如果存在“敌人”类的公共财产,则可能可以访问。 But you are not doing it in a right way. 但是,您的做法不正确。 You should make these properties private, then create public methods(getter/setter) to access the properties of those objects. 您应该将这些属性设为私有,然后创建公共方法(getter / setter)以访问那些对象的属性。

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

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