如何在JAva中使用从另一个方法创建的对象?

时间:2013-04-27 11:32:03

标签: java class methods

您好我正在尝试将我从方法objectAobjectB创建的ListA()ListB()传递给DoSomething方法,有人可以指导我该怎么做?

public static void main(String[] args) 
{
myclass test = new myclass();
test.ListA(args[0]);
test.ListB(args[1]);            
test.DoSomething(objectA, objectB);
}

public void ListA(String aaa){
objectA = Sting[];
//other codes goes here...
}   

public void ListB(String bbb){
objectB = Sting[];
//other codes goes here...
}

public static void DoSomething(List<String>objectA, List<String>objectB}{
//other codes goes here...
}

3 个答案:

答案 0 :(得分:1)

为什么不直接返回您创建的对象以使用DoSomething函数?

您好我正在尝试将从方法ListA()和List B()创建的objectA和objectB传递给DoSomething方法,有人可以指导我如何执行此操作吗?

public static void main(String[] args) 
{
myclass test = new myclass();
List<String> objectA = test.ListA(args[0]);
List<String> objectB = test.ListB(args[1]);         
test.DoSomething(objectA, objectB);
}

public List<String> ListA(String aaa){
List<String> generatedA;
//Generate your object...
return generatedA;
}   

public List<String> ListB(String bbb){
List<String> generatedB;
//Generate your object...
return generatedB;
}

public static void DoSomething(List<String>objectA, List<String>objectB}{
//other codes goes here...
}

答案 1 :(得分:1)

您希望ListAListB方法返回其值,然后将其传递给DoSomething方法。

这里有关于Java方法的初学者教程:homeandlearn 在这里:Java Doc

答案 2 :(得分:0)

为了能够通过它们你应该返回:

public static void main(String[] args) 
{
    myclass test = new myclass();    
    test.DoSomething(test.ListA(args[0]), test.ListB(args[1]));
}

public List<String> ListA(String aaa){
    objectA = String[];
    //other codes goes here...
}   

public List<String> ListB(String bbb){
    objectB = String[];
    //other codes goes here...
}

public static void DoSomething(List<String>objectA, List<String>objectB}{
    //other codes goes here...
}

或者让他们成员:

List<String> objectA;
List<String> objectB;

public static void main(String[] args) 
{
myclass test = new myclass();
test.ListA(args[0]);
test.ListB(args[1]);        
test.DoSomething();
}

public List<String> ListA(String aaa){
this.objectA = Sting[];
//other codes goes here...
}   

public List<String> ListB(String bbb){
this.objectB = String[];
//other codes goes here...
}

public static void DoSomething(}{
   this.objectA
   this.objectB
//other codes goes here...
}