Java银行示例
我创建了一个BankController.java类来输入并检索数据 包nimit.bankJava银行示例,java,hashmap,Java,Hashmap,我创建了一个BankController.java类来输入并检索数据 包nimit.bank 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>
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;
}
}
}
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;
}
}
我的疑问是,System.out.printlnMDB.getMDB.entityCustomer,key;将null作为输出。我不能理解。我将密钥作为对象类型传递。为什么不在每个案例后添加一个中断?这是一个很好的做法,可以避免以后发生任何震动。不管怎样,我运行了你的程序,得到了客户的反对意见,你需要覆盖toString…是的。那么你期待什么呢?将其强制转换到对象,然后从Pojo中获取值,类似于下面的System.out.printlnCustomerMDB.getMDB.entityCustomer,key.getName;哦,好的。知道了。我最好把文件再看一遍。谢谢你,我真的很感谢你的帮助。
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));
}
}