【发布时间】:2020-02-06 20:52:41
【问题描述】:
A类
public class Customer {
// Add instance varables
private String lName;
private String fName;
private String address;
private String zip;
// A constructor that initializes the last name, first name, address, and zip code.
public Customer(String lN, String fN, String addr, String zi) {
lName = lN;
fName = fN;
address = addr;
zip = zi;
}
// setAccount(Account a) - Sets the Account for this customer
}
// getAccount() - Returns a reference to the Account object associated with this customer
public Account getAccount(){
return();
}
}
我不知道如何从另一个类中“引用”一个对象。我无法创建该对象,因为我希望一切都是通用的,并且能够在以后创建并使这两个类正确地相互关联。
B类
public class Account {
// Add instance variables
private String accountNumber;
private double balance;
private Customer customer;
// A constructor that initializes the account number and Customer, and sets the blance to zero.
public Account(String aN, Customer c) {
accountNumber = aN;
balance = 0.00;
customer = c;
}
所以我不明白如何在A类中创建set account和get account方法
【问题讨论】:
-
您正在尝试在此处创建循环引用;记住
Customer可能有多个Account。
标签: java class associations