简体   繁体   English

如何从 JAVA 中的另一个文件访问 class 的变量

[英]How to access variables of a class from another file in JAVA

I am new to java and I am trying to work a simple program.我是 java 的新手,我正在尝试编写一个简单的程序。 I have created a class and defined the variables like this我创建了一个 class 并定义了这样的变量

public class Animal {

    private String age;
    private String color;
    Breed breed;

    public String getAge() {
        return age;
    }

    public void setAge(String age) {
        this.age = age;
    }

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }

    public Breed getBreed() {
        return breed;
    }

    public void setBreed(Breed breed) {
        this.breed = breed;
    }
}

class Breed {
    private String afador;
    private String afaird;
    private String aussiepom;

    public String getAfador() {
        return afador;
    }

    public void setAfador(String afador) {
        this.afador = afador;
    }

    public String getAfaird() {
        return afaird;
    }

    public void setAfaird(String afaird) {
        this.afaird = afaird;
    }

    public String getAussiepom() {
        return aussiepom;
    }

    public void setAussiepom(String aussiepom) {
        this.aussiepom = aussiepom;
    }
}

I want to access the variables in class "BREED" from ANOTHER file, with "animal.breed..." which does not work.我想从另一个文件访问 class "BREED" 中的变量,其中 "animal.breed..." 不起作用。 How can I access the variables in the "BREED" class??如何访问“BREED”class 中的变量? I am not sure if it is because I have the getter and setters in this file, kindly advise on the same.我不确定是不是因为我在这个文件中有 getter 和 setter,请同样提出建议。

I think I see what you are trying to do, however, you did not provide any sample code so I will be answering based on assumptions.我想我知道您要做什么,但是,您没有提供任何示例代码,因此我将根据假设进行回答。

To start, you are trying to access a variable from inside of the Breed class through the getter methods.首先,您尝试通过 getter 方法从 Breed class 内部访问变量。 Lets look at some code.让我们看一些代码。

// I'm assuming this is similar to what you have set up.

// Make a new breed
Breed breed = new Breed();

// Setting the breed to Afador through your methods
breed.setAfador("Afador");


// Make a new animal
Animal animal = new Animal();

// Set our animal's breed to our newly made breed
animal.setBreed(breed);

You mentioned you got the getBreed() method to work, so you must have done something along the lines of你提到你让 getBreed() 方法工作,所以你一定做了一些类似的事情

animal.getBreed();

You then mentioned you could not get getAfaird() method to work, so I assume you did something like this然后你提到你无法让 getAfaird() 方法工作,所以我假设你做了这样的事情

animal.getAfaird();

This does not work since getAfaird() does not exist within the Animal class but rather the Breed class.这不起作用,因为动物 class 中不存在 getAfaird(),而是品种 class 中不存在。 You can only use this getAfaird() method on Breed objects.您只能在 Breed 对象上使用此 getAfaird() 方法。 So you would want to do something like this所以你会想做这样的事情

animal.getBreed().getAfaird();

You must be user the get method:您必须是 get 方法的用户:

Animal animal = new Animal();
Breed breed = animal.getBreed();

Another option is declare public the breed attribute, but not is a good practique另一种选择是公开品种属性,但不是一个好习惯

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

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