简体   繁体   English

Java 类如何从条目 class 中获取信息?

[英]How do Java classes get information from the Entry class?

So lets say that in my entry point class (ie the class which runs when the program starts (which has the public static void main(String args[]) function). In that class I have this variable: So lets say that in my entry point class (ie the class which runs when the program starts (which has the public static void main(String args[]) function). In that class I have this variable:

private ArrayList<String> myData=new ArrayList<String>();

This class instantiates another class, which needs to have access to the myData member of the entry point class.这个 class 实例化了另一个 class,它需要访问入口点 class 的myData成员。 How can it retrieve this arraylist?它如何检索这个 arraylist?

Edit: To clarify, in the main() method I could do this:编辑:为了澄清,在main()方法中我可以这样做:

SomeOtherClass myClass=new SomeOtherClass();

and then I could do:然后我可以这样做:

myClass.someMethod();

however, in the myClass object, how could I perform a method/retrieve something from the entry class, which instantiated the myClass object?但是,在myClass object 中,我如何从条目 class 中执行方法/检索某些内容,该条目实例化了myClass object?

The best way to give the class you instantiate access to myData would be to pass it into the constructor when it is created.让 class 实例化访问myData的最佳方法是在创建时将其传递给构造函数。

Then, in your constructor, you can save the ArrayList into a member variable of the class.然后,在您的构造函数中,您可以将 ArrayList 保存到ArrayList的成员变量中。

For example, your object constructor will look like:例如,您的 object 构造函数将如下所示:

private ArrayList<String> myData;

public YourObjConstructor(ArrayList<String> data){
    myData = data;
}

It sounds like your entry point is still static when it calls some other class, but your ArrayList is a member of an instance of it.听起来你的入口点仍然是 static 当它调用其他一些 class 时,但你的 ArrayList 是它的一个实例的成员。 You need to move out of the static world and into instances.您需要离开 static 世界并进入实例。

I'd refactor your main method into a private construtor, and put in a new main() which launches it as a new instance.我会将您的 main 方法重构为私有构造函数,并放入一个新的 main() 将其作为新实例启动。

Note that this code is very rough, but it should serve to illustrate what you need to do.请注意,此代码非常粗略,但它应该有助于说明您需要做什么。

public class EntryPoint {
    private ArrayList<String> myData=new ArrayList<String>();

    public static void main( String[] args ) {
        EntryPoint ep = new EntryPoint();
        ep.init();
    }

    private void init() {
        // Populate myData perhaps?

        SomeOtherClass myClass=new SomeOtherClass();
        myClass.someMethod( this );
    }

    public List<String> getMyData() {
        return myData;
    }
}   

public class SomeOtherClass {
    public void someMethod( EntryPoint entry ) {
        List<String> data = entry.getMyData();
        // do stuff with data..!
    }
}

The class containing main() is just an ordinary class.包含main()的 class 只是一个普通的 class。 In your case, you'd have to make myData public and possibly static (or, of course, add an accessor).在您的情况下,您必须public myData并且可能公开static (或者,当然,添加访问器)。 Just like you'd do with any other class.就像您对任何其他 class 所做的那样。

You could also pass an Entry object to the other class, like this:您还可以将Entry object 传递给另一个 class,如下所示:

public static void main(String[] args) {
    Entry entry = new Entry();
    SomeOtherClass myClass=new SomeOtherClass(entry);
    // continue as before
}

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

相关问题 如何从SIP类获取SDP信息(在Java中)? - How to get the SDP information from sip class (in java)? Java到Java字节码编译器:如何获取Java类库信息? - Java to Java Bytecode Compiler: How Do I Get Java Class Library Information? 如何从JVM获取有关加载哪些类以及所有已加载类的规范的信息? - How can I get information from JVM about what classes are loaded along with the specs of all the loaded class? 如何在Java中初始化类层次结构的类? - How classes of class hierarchy get initialized in java? 如何创建动态jar文件以从入口点(主)获取多个类及其方法的访问而无需导入Java? - How to create dynamic jar file to get access of multiple classes and its methods from an entry point(main) without importing in java? 如何获取从一个类到另一个Action Listener类的变量信息? - How do I get information of the variables from one class to another Action Listener class? 如何从.class重新创建Java类? - How to recreate java classes from .class? 如何从Java网页中获取信息? - How to get information from a webpage in Java? 如何从Java中的Process获取信息? - How to get information from Process in java? 如何通过 Java 反射从父类中获取嵌套类 - How to get nested classes from a class from the parent one via Java Reflection
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM