Java 如何获取类的实例?
我有以下代码Java 如何获取类的实例?,java,class,Java,Class,我有以下代码 class driver{ static BankAccount GetAccount(Customer customer, char c) { BankAccount accSrc = customer.S; // savings account if (c =='S') { accSrc = customer.S; // loan ac
class driver{
static BankAccount GetAccount(Customer customer, char c) {
BankAccount accSrc = customer.S;
// savings account
if (c =='S') {
accSrc = customer.S;
// loan account
} else if (c =='L') {
accSrc = customer.L;
// checking account
} else if (c =='C') {
accSrc = customer.C;
// auto loan account
} else if (c =='A') {
accSrc = customer.A;
}
return accSrc;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Customer forrest = new Customer("Forrest Gump", 1, "42 New Street, New York, New York"); // me
Customer random = new Customer("Random Name", 2, "44 New Street, New York, New York"); // imaginary partner
//try{
String input = JOptionPane.showInputDialog("Please enter your transaction information: ");
Scanner s = new Scanner(input);
int id = Integer.parseInt(s.next());
char action = Character.toUpperCase((s.next().charAt(0)));
char accSrc = ' ';
char accDest = ' ';
double amount = 0;
if(action == 'T'){
amount = s.nextDouble();
accSrc = s.next().charAt(0);
accDest = s.next().charAt(0);
}else if(action == 'G' || action == 'I'){
accSrc = s.next().charAt(0);
}else{
//if D,W
amount = s.nextDouble();
accSrc = s.next().charAt(0);
}
//}catch (IOException e){
//}
if(id==1){
return forrest;
}else if(id == 2){
return random;
}
BankAccount src = GetAccount(forrest, accSrc);
System.out.print(src.getOwner().name);
if(action == 'T'){
BankAccount dst = GetAccount(forrest, accDest);
src.transfer(amount, dst);
.
..
}
class Customer{
protected String name;
protected int id;
protected String address;
protected BankAccount C = new BankAccount(id, this, 0);
protected BankAccount S = new BankAccount(id, this, 0);
protected BankAccount A = new BankAccount(id, this, 0);
protected BankAccount L = new BankAccount(id, this, 0);
...
}
当前im硬编码BankAccount
src=GetAccount(forrest,accSrc)代码>如何继续编写代码,使其返回给定id号的客户实例(例如1个给定forrest返回,2个给定random返回)?您需要将您的内容存储在a中,然后根据其索引检索列表项。您需要将您的内容存储在a中,然后根据其索引检索列表项。将您的类放入映射中
Map<Integer, Customer> classMap = new HashMap<Integer, Customer>();
Customer forrest =
new Customer("Forrest Stallings", 1, "42 New Street, New York, New York");
classMap.put(1, forrest );
Customer random =
new Customer("Random Name", 2, "44 New Street, New York, New York");
classMap.put(2, random );
把你的课程放在地图上,例如
Map<Integer, Customer> classMap = new HashMap<Integer, Customer>();
Customer forrest =
new Customer("Forrest Stallings", 1, "42 New Street, New York, New York");
classMap.put(1, forrest );
Customer random =
new Customer("Random Name", 2, "44 New Street, New York, New York");
classMap.put(2, random );
更好
Customer customers[] = { forrest, random };
我可以使用customer[id]
更好地调用实例
Customer customers[] = { forrest, random };
我可以使用customer[id]
@user133466调用该实例。根据您的需求,可能会有更多的方法,但考虑到场景,这是最简单的方法,可以达到目的。@user133466有很多种方法,这确实是一种。您可以使用列表、数组、文件甚至数据库。事实上,如果您仍在硬编码,这是最简单的解决方案。@user133466根据您的需求,可能会有更多的解决方案,但鉴于该场景,这是最简单的解决方案,并且可以达到目的。@user133466有很多种方法,这确实是一种。您可以使用列表、数组、文件甚至数据库。事实上,如果你还在硬编码的话,这是最简单的解决方案。