简体   繁体   English

如何获取已经在另一个 class 中设置的实例值?

[英]How can I get a value of instance which is already set in another class?

I'm a new to Java.我是 Java 的新手。 I have 3 classes: 1-DataHolder 2-SetToDateHolder 3-GetFromDataHolder;我有 3 个类:1-DataHolder 2-SetToDateHolder 3-GetFromDataHolder; In the "SetToDateHolder" class, I set the value to dataholder object, and I trying to access it in the "GetFromDataHolder" class, but it got null value, I know it happens because of new instance created. In the "SetToDateHolder" class, I set the value to dataholder object, and I trying to access it in the "GetFromDataHolder" class, but it got null value, I know it happens because of new instance created. Can you help me to realize that I can get the value of instance which I have set in another class.你能帮我意识到我可以得到我在另一个 class 中设置的实例的值。

These are the classes:这些是类:

public class DataHolder {

    private String myName;

    public String getMyName() {
        return myName;
    }

    public void setMyName(String myName) {
        this.myName = myName;
    }
}

public class SetToDataHolder {

    public static void main(String[] args) {
        DataHolder dataHolder = new DataHolder();
        dataHolder.setMyName("Wesley");
    }

}

public class GetFromDataHolder{
    
    public static void main(String[] args) {
        DataHolder dataHolder = new DataHolder();
        System.out.println(dataHolder.getMyName());
    }

}

I don't really understand why you need 2 additional classes as you already have the getter / setter in your DataHolder class, but here's how you should be able to proceed your logic:我真的不明白为什么你需要 2 个额外的类,因为你的 DataHolder class 中已经有了 getter / setter,但这里是你应该如何继续你的逻辑:

public class DataHolder {

    private String myName;

    public String getMyName() {
        return myName;
    }

    public void setMyName(String myName) {
        this.myName = myName;
    }
}

public class SetToDataHolder {
    
    private DataHolder dataHolder;

    public SetToDataHolder(DataHolder dataHolder){
        this.dataHolder = dataHolder;
    }

    public void setMyName(String myName) {
        this.dataHolder.setMyName(myName);
    }
}

public class GetFromDataHolder{
    
    private DataHolder dataHolder;

    public GetFromDataHolder(DataHolder dataHolder){
        this.dataHolder = dataHolder;
    }

    public String getMyName() {
        this.dataHolder.getMyName();
    }
}

public class MainClass{
    
    public static void main(String[] args){
        DataHolder dataHolder = new DataHolder();

        SetToDataHolder setToDataHolder = new SetToDataHolder(dataHolder);
        setToDataHolder.setMyName("Wesley");

        GetFromDataHolder getFromDataHolder = new GetFromDataHolder(dataHolder);
        System.out.println(getFromDataHolder.getMyName());
    }
}

The idea is that your SetToDataHolder and GetFromDataHolder, have access to the same instance of DataHolder.这个想法是您的 SetToDataHolder 和 GetFromDataHolder 可以访问相同的 DataHolder 实例。

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

相关问题 如何在另一个 class 中获取实例? 在 java - How can I get an instance in another class? in java 我如何从Java集获取返回值 <String> 在java的另一个类中 - how i can get the return value from java set<String> in another class in java 我如何从另一个班级获得价值 - How can i get a value to class from another class 我怎么知道一个类的实例是否已经存在于内存中? - How can I know whether an instance of a class already exists in memory? 如何为已经运行的实例更改Java类的代码? - How can I change the code for a Java class for an already running instance? 如何从另一个类的静态方法获取抽象类的子类的实例? - How can I get an instance of a subclass of abstract class from a static method of another class? 我如何从另一个人那里获得行动课的价值 - How can I get a value of an action class from another 如何通过课程获取实例? - How I can get a instance by a class? 如何获取在 servlet 过滤器中的静态块内启动的静态类的实例? - How can I get the instance of a static class which is initiated inside static block in servlet filter? 我如何获取另一个类的方法中获取的值的数组 - How I can get array which values were get in method of another class
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM