2014-03-25 36 views
0

長話短說,我想要一種方法來返回兩個項目。我想我已經學會了用嵌入式類來做這件事的最好方法。儘管我在結構和語法方面遇到困難,但如何訪問信息。需要幫助訪問嵌入類中的信息

真的,我想要做的是有一個方法返回一個數組字符串[]和一個字符串。如果你能想到一個更簡單的方法來做到這一點,我會非常有興趣聽到它。

非常感謝你對你幫助

感謝

import java.util.*; 

public class test 
{ 

public test() 
{ 
} 

public class SQLarguments //embedded class 
{ 
    String[] columns; 
    String table; 

    public SQLarguments(String table, String... columns) 
    { 
     this.table = table; 
     this.columns = columns; 
    } 
} 

public SQLarguments arguments(String table, String... columns) 
{ 
    SQLarguments testArgs = new SQLarguments(table,columns);   
    return testArgs; 
} 

public static void main(String[] args) 
{ 
    test t1 = new test(); 
    t1.arguments("table","col 1","col 2", "col 3"); 
    System.out.println(.arguments[0]); 
    System.out.println("test"); 
}//end main 

}//end class 

回答

2

我重組你的代碼把測試方法在一起,SQLarguments方法一起使用。我將主類的名稱更改爲Test,因爲Java中的類名以大寫字母開頭。

我在SQLarguments類中添加了兩個getter方法,因此您可以檢索在構造函數中設置的值。我在你的Test main方法中使用了一個getter方法。

下面的代碼:

public class Test { 

    public Test() { 

    } 

    public SQLarguments arguments(String table, String... columns) { 
     SQLarguments testArgs = new SQLarguments(table, columns); 
     return testArgs; 
    } 

    public static void main(String[] args) { 
     Test t1 = new Test(); 
     SQLarguments arguments = 
       t1.arguments("table","col 1","col 2", "col 3"); 
     System.out.println(arguments.getColumns()[0]); 
     System.out.println("test"); 
    } //end main 

    public class SQLarguments {  // Embedded class 
     String[] columns; 
     String  table; 

     public SQLarguments(String table, String... columns) { 
      this.table = table; 
      this.columns = columns; 
     } 

     public String[] getColumns() { 
      return columns; 
     } 

     public String getTable() { 
      return table; 
     } 

    } 

} 
+0

這正是我試圖做。非常感謝你真棒! – demuro1