长话短说,我想要一个返回两个项目的方法。我认为我学到的最好方法是使用嵌入式类(class)。不过,我在结构和语法以及如何访问信息方面遇到了困难。
我真正想做的是让一个方法返回一个数组 String[] 和一个 String。如果您能想出一种更简单的方法来做到这一点,我真的很想听听。
非常感谢您的帮助
谢谢
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
最佳答案
我重新组织了您的代码,将测试方法和 SQLarguments 方法放在一起。我将主类的名称更改为Test,因为Java中的类名都是大写字母开头。
我向 SQLarguments 类添加了两个 getter 方法,以便您可以检索在构造函数中设置的值。我在您的测试主方法中使用了一种 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;
}
}
}
关于java - 需要帮助访问嵌入式类中的信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22624632/