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

时间:2010-07-16 07:11:38

标签: java

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

注意:如果我将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
}

请提出建议。提前谢谢。

4 个答案:

答案 0 :(得分:2)

您缺少一些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();
    }
}

答案 1 :(得分:1)

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

主要有两种选择:

a)使用getter方法。

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

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

b)使用公共成员变量。

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

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

我建议你使用第一个。

答案 2 :(得分:1)

要回答您的问题,value是Bbb.method方法中的局部变量。要从另一个类访问它,它必须是类的实例变量(在类中声明但在任何方法之外)并且可访问(public或package(也称为默认),或者与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
    }
}

我在语法和标准代码约定中添加了一些额外的注释,这些注释可以帮助您学习Java。

答案 3 :(得分:0)

您可以简单地声明变量并在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
     }
}