我有两个名为 x.java
和 y.java
的 Java 类。
在x.java
中我有一个 protected 方法:
protected ResultSet populateSpeciesList(ResultSet results)
在这个 protected 方法内部,我有一个 if 语句:
if (fsTitle != null)
{
sp.setFireStudyTitle("Available");
String sppAcronym = results.getString("ACRONYM");
FireStudyQueryBuilder qb = new FireStudyQueryBuilder();
this.magicString = qb.buildMagicString();
}
现在在 y.java
中,我有一个如下所示的方法:
String buildMagicString()
{
String sppAcronym = getAcronym();
String newQueryString = a bunch of SQL;
return newQueryString;
}
问题是,我无法在其他类中使用 sppAcronym,因为它无法识别。
所以我的问题是,如何将变量从类传递到另一个类?
谢谢。
最佳答案
你不能像下面这样重写你的方法 buildMagicString() 吗?
String buildMagicString(String sppAcronym) //This allows the caller to pass a argument to this method.
{
System.out.println(sppAcronym); //This statement will compile because sppAcronym is a valid member of this method.
String newQueryString = a bunch of SQL;
return newQueryString;
}
然后您只需按如下方式调用该方法:
if (fsTitle != null)
{
sp.setFireStudyTitle("Available");
String sppAcronym = results.getString("ACRONYM");
FireStudyQueryBuilder qb = new FireStudyQueryBuilder();
this.magicString = qb.buildMagicString(sppAcronym); //Passes the sppAcronym object to the buildMagicString() method.
}
关于java - 在 Java 中如何将变量从一个类传递到另一个类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18118253/