2010-07-16 71 views
-4

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

注:如果我把字符串值变量作为公共,那么它显示一些错误。

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 
} 

请给出建议。提前致谢。

+22

您需要一个Java教程。 – bakkal 2010-07-16 07:17:20

回答

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; 
//... 

我推荐你使用第一个。

+0

感谢您的修复,我的不好。 – Nubsis 2010-07-16 13:15:33

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

要回答你的问题,值是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。

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 
    } 
} 
相关问题