方法getVal
的返回值报告错误:
必须返回类型为test1的值。
我不确定它为什么会抱怨返回类型,因为我认为get
会给你相应键的值。请帮我理解这个逻辑。
public class test1 {
//There is some class named test1
public test1() { //Default Constructor
}
public test1 getVal(String name) {
return sample.get(name);
}
}
public class test {
public test() {
HashMap<String, test1> sample = new Hashmap<>();
sample.put("Name", new test1());
}
}
名为sample
的HashMap在getVal
方法中不可见。您可以将其作为参数添加到该方法中,或者使HashMapsample
对getVal
方法可见。