如何使用java将一个类的变量使用到另一个类中?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3262670/
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-08-13 17:50:10  来源:igfitidea点击:

how to use the variable of one class into another class using java?

java

提问by Karthi

Ihave two class files. In that first file, i stored aaa class firl and in the next text file i store the class file bbb.Io want to use the variable of aaa class into bbb class. How to use use it.

我有两个类文件。在第一个文件中,我存储了 aaa 类 firl,在下一个文本文件中我存储了类文件 bbb.Io 想将 aaa 类的变量使用到 bbb 类中。如何使用它。

Note :If i put String value variable as public then it shows some error.

注意:如果我将 String 值变量设置为 public 那么它会显示一些错误。

class aaa{
    public void method{
        String value="as some output";
        string other="it has some output";
    }
}
public static void main(String args[]){
    aaa obj=new first();
    bbb object=new second();
}

class bbb{
    aaa obj2=new aaa();
    System.out.println(obj2.value); //It gives error here also
}

Please suggest ideas. Thanks in advance.

请提出想法。提前致谢。

回答by mahju

Your class aaa does not have a public member variable that is called value. You have a local variable value in your method, which you will not be able to use.

您的类 aaa 没有称为 value 的公共成员变量。您的方法中有一个局部变量值,您将无法使用该值。

There are mainly two options:

主要有两种选择:

a) Use a getter method.

a) 使用 getter 方法。

class Aaa
{
    //...
    public String getValue()
    {
        return this value;
    }
}

//...
Aaa a = new Aaa();
String test = a.getValue();
//...

b) Use a public member variable.

b) 使用公共成员变量。

class Foo
{
   // ...
   public String value = "bar"; 
   //...
}

//...
Aaa a = new Aaa();
String test = a.value;
//...

I recommend you use the first one.

我建议你使用第一个。

回答by Cambium

You are missing some basic knowledge of Java, maybe read up a few tutorials on the web.

您缺少一些 Java 的基本知识,也许在网上阅读了一些教程。

// use capitals for the first letter, conventions are good ;)
public class Aaa{
    // I think this is what you meant, you want member variables, add public if you
    // want them to be accessible anywhere
    public String value="as some output";
    public String other="it has some output";

    // missing brackets
    public void method() {
        // do something/anything
    }
}

public class Bbb{
    // you need to put this in a main... without getting exceptions
    public static void main(String args[]){
        Aaa obj2=new Aaa();
        // now you can access the field value, since it's public
        System.out.println(obj2.value); //error should be gone now
    }
}

public class TestMain{
    public static void main(String args[]){
        // first and second aren't classes, you meant Aaa and Bbb?
        Aaa objA=new Aaa();
        Bbb objB=new Bbb();
    }
}

回答by Chadwick

To answer your question, value is a local variable within the Bbb.methodmethod. To access it from another class, it must be an instancevariable of the class (declared in the class but outside any method) and accessible (public, or package (aka default), or private with getter/setter methods)

为了回答您的问题, value 是方法中的一个局部变量Bbb.method。要从另一个类访问它,它必须是该类的实例变量(在类中声明但在任何方法之外)并且可访问(公共或包(又名默认),或使用 getter/setter 方法私有)

// note that I've renamed classes to follow the convention of uppercasing class names.
// this makes the code much easier to read.
class Aaa {
    public String value = "instance value initialized when the class loads (first use)";
    public String other = null;

    // a method declaration must have parentheses, even if it takes no parameters
    public void method() {
        other = "instance value, initially null, set by calling method";
    }
}

class Bbb {
    Aaa aaaInBbb = new Aaa();

    public void method(){
        // every statement (except variable declarations) must be in a method
        System.out.println(aaaInBbb.value); // access the public value in aaaInBbb
    }
}

class C {
    // note that main(...) must be in a class as well - as all methods in Java must
    public static void main(String[] args) { // convention also puts [] next to the type
        Aaa aaa = new Aaa(); // this variable is never used.
        Bbb bbb = new Bbb();

        bbb.method();  // causes instance of Bbb to print out aaaInBbb.value
    }
}

I've added some additional comments on syntax and standard code conventions that will help you as you learn Java.

我已经添加了一些关于语法和标准代码约定的附加注释,它们将有助于您学习 Java。

回答by pooja majoka

You can simple declare the variable and initialize the value of variable in method1 and finally extends that class.

您可以简单地声明变量并在 method1 中初始化变量的值,最后扩展该类。

class aaa{
String value;
    public void method(){
         value="as some output";       
    }
}

class bbb extends aaa{  
     public void method2(){
         System.out.println(value); //Output is "as some output";      
    }      

    public static void main(String as[]){
     bbb obj2=new bbb();
     obj2.method();   //getting the value of string value
     obj2.method2(); //print the value in method2
     }
}