package nimit.bank;
import java.util.HashMap;
public class MDB {
public final static int entityCustomer=1;
public static HashMap<Object, Object> hmapCustomer = new HashMap<Object, Object>();
public final static int entityAccount=2;
public static HashMap<Object, Object> hmapAccount = new HashMap<Object, Object>();
public final static int entityBank=3;
public static HashMap<Object, Object> hmapBank = new HashMap<Object, Object>();
public static Object get(int entity,Object key) {
Object obj=null;
switch(entity){
case entityCustomer: obj=hmapCustomer.get(key);
break;
case entityAccount: obj=hmapAccount.get(key);
break;
case entityBank: obj=hmapBank.get(key);
break;
}
return obj
}
public static void put(int entity, Object key,Object obj ){
switch(entity){
case entityCustomer: hmapCustomer.put(key,obj);
break;
case entityAccount: hmapAccount.put(key, obj);
break;
case entityBank: hmapBank.put(key,obj);
break;
}
}
}
这是一个银行示例,我使用哈希图来存储数据。我创建了一个 Customer.java 类,如下所示。
包nimit.bank;
public class Customer {
private String name;
private String address;
private long phone_no;
public String getName() {
return name;
}
public String getAddress() {
return address;
}
public long getPhone_No() {
return phone_no;
}
public Customer(String _Name, String _Address, long _phone) {
name = _Name;
address = _Address;
phone_no = _phone;
}
}
我创建了一个 BankController.java 类来输入数据并检索它。
包nimit.bank;
public class BankController {
public static void main(String[] args) {
Customer C = new Customer("nimit", "Jane Street", 26711610);
Object key = 54321;
MDB.put(MDB.entityCustomer, key, C);
System.out.println(MDB.get(MDB.entityCustomer, key));
}
}
我的疑问是System.out.println(MDB.get(MDB.entityCustomer,key)); 给出 null 作为输出。我无法理解。我将密钥作为对象类型传递。