2013-04-27 83 views
-1

嗨我想通過objectAobjectB,我創建從方法ListA()ListB()DoSomething方法,可有人請指導我如何做到這一點?如何使用JAva中另一種方法創建的對象?

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... 
} 

回答

1

爲什麼不直接返回創建的對象與DoSomething函數一起使用?

嗨我試圖將方法ListA()和列表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

你會希望ListAListB方法返回它們的值,然後將那些到DoSomething方法。

初學者教程Java方法在這裏:homeandlearn 這裏:Java Doc

+0

或者將值存儲在類本身中。 – skuntsel 2013-04-27 11:42:46

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... 
} 
相關問題