java - 如何从Java中的另一个方法访问对象?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29678813/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 15:43:17  来源:igfitidea点击:

How can i access an object from another method in java?

java

提问by Lloyd aaron

I have the object numberlist that i created in create() method and i want to access it so i can use it in the question() method.

我有在 create() 方法中创建的对象编号列表,我想访问它以便我可以在 question() 方法中使用它。

Is there another way to do this that I probably missed? Am I messing something up? If not, how should I do this to get the same functionality as below?

有没有另一种方法可以做到这一点,我可能错过了?我搞砸了什么吗?如果没有,我应该如何做到这一点以获得与下面相同的功能?

private static void create() {
    Scanner input = new Scanner(System.in);

    int length,offset;

    System.out.print("Input the size of the numbers : ");
     length = input.nextInt();

     System.out.print("Input the Offset : ");
     offset = input.nextInt();

    NumberList numberlist= new NumberList(length, offset);




}


private static void question(){
    Scanner input = new Scanner(System.in);

    System.out.print("Please enter a command or type ?: ");
    String c = input.nextLine();

    if (c.equals("a")){ 
        create();       
    }else if(c.equals("b")){
         numberlist.flip();   \ error
    }else if(c.equals("c")){
        numberlist.shuffle(); \ error
    }else if(c.equals("d")){
        numberlist.printInfo(); \ error
    }
}

采纳答案by Aziel Ferguson

While interesting, both of the answers listed ignored that fact that the questioner is using static methods. Thus, any class or member variable will not be accessible to the method unless they are also declared static, or referenced statically. This example:

虽然有趣,但列出的两个答案都忽略了提问者使用静态方法的事实。因此,任何类或成员变量都不能被方法访问,除非它们也被声明为静态或静态引用。这个例子:

public class MyClass {
    public static String xThing;
    private static void makeThing() {
        String thing = "thing";
        xThing = thing;
        System.out.println(thing);
    }
    private static void makeOtherThing() {
        String otherThing = "otherThing";
        System.out.println(otherThing);
        System.out.println(xThing);
    }
    public static void main(String args[]) {
        makeThing();
        makeOtherThing();
    }
}

Will work, however, it would be better if it was more like this...

会起作用,但是,如果它更像这样会更好......

public class MyClass {
    private String xThing;
    public void makeThing() {
        String thing = "thing";
        xThing = thing;
        System.out.println(thing);
    }
    public void makeOtherThing() {
        String otherThing = "otherThing";
        System.out.println(otherThing);
        System.out.println(xThing);
    }
    public static void main(String args[]) {
       MyClass myObject = new MyClass();
       myObject.makeThing();
       myObject.makeOtherThing();
    }
}

回答by Lawrence Aiello

You would have to make it a class variable. Instead of defining and initializing it in the create() function, define it in the class and initialize it in the create() function.

您必须将其设为类变量。不是在 create() 函数中定义和初始化它,而是在类中定义它并在 create() 函数中初始化它。

public class SomeClass {
    NumberList numberlist; // Definition
    ....

Then in your create() function just say:

然后在你的 create() 函数中说:

numberlist= new NumberList(length, offset);  // Initialization

回答by MTCoster

Declare numberListoutside your methods like this:

numberList像这样在你的方法之外声明:

NumberList numberList;

Then inside create()use this to initialise it:

然后在里面create()使用它来初始化它:

numberList = new NumberList(length, offset);

This means you can access it from any methods in this class.

这意味着您可以从此类中的任何方法访问它。