我有两个名为x.java
和的Java类y.java
。
在x.java
我有一个受保护的方法:
protected ResultSet populateSpeciesList(ResultSet results)
在此受保护方法的内部,我有一个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.
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句