【发布时间】:2020-03-23 13:11:21
【问题描述】:
好吧,假设我有两节课。一个顾客和一个商店。
// In the class customer, I have 3 instance variables.
private String aFullName;
private String address;
private char age;
// Then in the constructor, I have initialised these too:
this.aFullName = fullName;
this.address = anAddress;
this.age = anAge;
//In my second class then, the shop...
//I only have one variable where I've referenced a map:
private Map<String, Customer> customers;
//My constructor:
public Shop()
{
super();
customers = new HashMap<>()
}
我的问题是:
在 shop 类中,我必须创建一个名为 addCustomer 的方法,该方法接受 4 个参数。它将首先创建一个客户实例,然后将其添加到我的名为 customers 的地图中。
参数不能改变,我的问题是当方法中的参数和客户类中的变量不同时,我对如何创建实例感到困惑
public void addCustomer(String memNo, String name, String address, char ageCat)
// where memNo is going to be the key.
如何创建实例并将其添加到客户引用的map中,以memNo为key?
然后,如果我正在测试这个方法,我应该能够将客户添加到地图,但使用这个方法 addCustomer
谢谢
【问题讨论】:
-
this.customers.put(memNo, new Customer(name,address,ageCat))?
标签: java class dictionary instance