简体   繁体   English

从主外部的方法访问类对象

[英]Access a class object from a method outside the main

Sorry if the title isn't as accurate as it should be or maybe even falesly translated on my part. 抱歉,标题不够准确,或者我翻译的内容有些含糊。 I'm german and a beginner in coding so take it easy one me. 我是德国人,也是编码方面的初学者,所以请放轻松。 So I coded a programm in JAVA. 所以我用JAVA编写了一个程序。 It is a programm for a bank account that I picked up in a book and tried to expand. 这是我在书中挑选并尝试扩展的银行帐户程序。 Now, to keep it simple in this thread I obviously reduced my example code on the problem. 现在,为了使该线程保持简单,我显然简化了有关该问题的示例代码。

public class Test1 {
public static void method(){
    k.setNumber(300);  // This is where the problem is. From this method 
                       // I cannot acces the in main created class 
                       // object from 'Test'
    }

public static void main(String[] args) {

    Test k = new Test();

    method();
    k.getNumber();

}

}

Now the class 'Test: 现在类“测试:

public class Test {

int number;

public int getNumber(){
    return number;
}

public void setNumber(int nr){
    number = nr;
}
}

So is there any way around it? 那么有什么办法解决吗? Otherwise I would have to write everything in the 'main', which is no problem in the case of this example, but in my original code thats not so easy to do. 否则,我将不得不将所有内容都写在“ main”中,在本示例中,这没有问题,但是在我的原始代码中,这样做并不那么容易。

Thanks for any help and advise. 感谢您的帮助和建议。 Hopefully this wasn't posted before because I already searched this site and the web. 希望这不是以前发布的,因为我已经搜索了该站点和网络。

Currently, k only belongs to the main method. 目前, k仅属于main方法。 To access it from other methods within the same class, you need to make it a class variable. 要从同一类中的其他方法访问它,您需要使其成为一个类变量。 This should be what you need: 这应该是您需要的:

public class Test1 {
    private static Test k;

    public static void method() {
        k.setNumber(300);
    }

    public static void main(String[] args) {
        k = new Test();
        method();
        k.getNumber();
    }
}

You can also use it as variable for your method : 您也可以将其用作方法的变量:

public class Test1 {

public static void method(Test test){
    test.setNumber(300); 
}

public static void main(String[] args) {
  Test k = new Test();

  method(k);
  k.getNumber();
 }

}

you need to define 您需要定义

Test K = new Test()

outside of either methods, ie main and method. 在任何一种方法之外,即main和method。 Mostly you need to make 'K' global to be visible to both methods. 通常,您需要使“ K”全局可见,以使两种方法均可见。

you can do it as: 您可以这样做:

    public class Test1 {
public static void method(Test k){
    k.setNumber(300);  // This is where the problem is. From this method 
                       // I cannot acces the in main created class 
                       // object from 'Test'
    }

public static void main(String[] args) {

    Test k = new Test();

    method(k);//pass this test class instance to method
    k.getNumber();

}

}

You can either set k to be a global variable or you can pass it in the parameters of method like so. 您可以将k设置为全局变量,也可以像这样在method的参数中传递它。

public static void main( String[] argv )
{
    final Test k = new Test();

    method( k );
    System.out.println(  k.getNumber() );
}

public static void method(Test k)
{
    k.setNumber( 2 );
}

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

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