我有主类 Library
我在其中创建:
HashMap<String, HashSet<String>> students_books = new HashMap<String, HashSet<String>>();
然后我将上课 Student
我将创建一个构造函数,它将 HashMap 作为参数,如下所示:
public class Student {
private Student(HashMap students_books){
然后,回到我的主类(库)中,我创建了一个 student
对象,我想将 HashMap 作为参数:
Student student = new Student(*HashMap as parameter*);
我没有找到的是如何做到这一点以及 Student
类如何知道我传递的 HashMap 类型,例如 <String, HashSet<String>>
原文由 Fosh 发布,翻译遵循 CC BY-SA 4.0 许可协议
首先,你的方法不是构造函数,因为它有返回类型,删除它的返回类型并公开你的构造函数。然后通过这样做强制他们传递你想要的 HashMap 类型
然后像这样传递它们