如何在Java中为另一个类中的方法执行外部方法调用?试图调用的方法是使用Hashmap的方法
//这是方法的名称。从程序外部存储的文本文件中读取信息的一种方法。该方法包含在名为FileHelper的类中。该类包含各种其他方法,以帮助执行基于程序之外的文件的读写任务如何在Java中为另一个类中的方法执行外部方法调用?试图调用的方法是使用Hashmap的方法,java,methods,syntax,Java,Methods,Syntax,//这是方法的名称。从程序外部存储的文本文件中读取信息的一种方法。该方法包含在名为FileHelper的类中。该类包含各种其他方法,以帮助执行基于程序之外的文件的读写任务 public HashMap<String, String> readAMap (String filename) { HashMap<String, String> map = new HashMap<>(); try (BufferedReader reader =
public HashMap<String, String> readAMap (String filename) {
HashMap<String, String> map = new HashMap<>();
try (BufferedReader reader =
new BufferedReader(new FileReader(filename))) {
String word;
word = reader.readLine();
while(word != null) {
String response = reader.readLine();
if(response != null) {
response = response.trim();
if(response.length() != 0) {
map.put(word, response);
}
else {
System.out.println("Blank response for " +
word + " in file " +
filename);
}
}
else {
System.out.println("Missing response for " +
word + " in file " +
filename);
}
word = reader.readLine();
}
}
catch(IOException e) {
System.out.println("Problem reading file: " + filename +
" in readAMap");
}
return map;
}
//这是我用来调用上述方法的方法。这个方法是另一个类
private void fileResponseMap()
{
FileResponseMap = FileHelper.HashMap<String, String>readAMap(JavaFile);
return FileResponseMap;
}
}
//我正试着去看电影
实例化提供readMap方法的类。
在实例上调用该方法
如果要从此处调用方法,请执行以下操作: 在所有事物实例之前,您的类ex:他的名字是FileResponseMap,如: 然后像这样调用您的方法:
`ycn.readAMap ("JavaFile");`.
您可以使用returnhashmap调用方法
您的readAMap方法不是静态的,因此无法以您在fileResponseMap中尝试的方式访问它
您可以将其声明为静态:
public static HashMap<String, String> readAMap (String filename) {
...
您还必须在返回HashMap时定义返回类型。请在提问时多做一点努力。解释代码,解释你的问题,请给我们一点帮助。
`ycn.readAMap ("JavaFile");`.
private HashMap<String, String> fileResponseMap()
{
FileResponseMap ycn = new FileResponseMap();
HashMap<String, String> hmfile = ycn.readAMap("JavaFile");
return hmfile ;
}
}
public static HashMap<String, String> readAMap (String filename) {
...
private HashMap<String, String> fileResponseMap()
{
FileHelper fileHelper = new FileHelper();
return fileHelper.readAMap(JavaFile);
}