我正在努力使此代码正常工作。这是我的代码。头等舱:
public class PersonalAccount extends Account{
private String cardNumber;
private String cardType;
public ArrayList<PersonalAccount> personalAccounts;
public int personal;
private PersonalAccount(String first, String last, String accountNumber, String cardNumber, String cardType){
super(first, last, accountNumber);
this.cardNumber = "";
this.cardType = "";
}
public void addPersonalAccount(PersonalAccount aPersonalAccount){
personalAccounts.add(aPersonalAccount);
}
public void getNumberOfPersonalAccounts(){
personal = personalAccounts.size();
}
public void listAccounts(){
for (PersonalAccount personalaccount : personalAccounts){
System.out.println("Personal Accounts");
System.out.println(personalaccount);
}
}
public void findAccount(){
int index = 0;
boolean found = false;
while(index < personalAccounts.size() && !found){
PersonalAccount personalaccount = personalAccounts.get(index);
if (personalaccount.getaccountNumber().equals(accountNumber)){
found = true;
}else{
index++;
}
}
}
}
尝试在另一个类中创建此类的实例时,它会创建PersonalAccount对象的实例。有没有办法解决这个问题?我对Java和BlueJ非常陌生,应该注意。
编辑:对不起,我应该澄清。我试图在另一个类中调用此类的方法。但是当宣布
PersonalAccount class1 = new PersonalAccount();
我收到错误消息:类PersonalAccount中的构造函数PersonalAccount无法应用于给定类型。
我试图在按钮单击时调用该方法(其中numAcc是按钮):
numAcc.addActionListener(new ActionListener()
{
公共无效actionPerformed(ActionEvent evt)
{
个人
个人= class1.getNumberOfPersonalAccounts();
}
});
最佳答案
您没有默认的构造函数,因此无法创建如下的PersonalAccount:
PersonalAccount class1 = new PersonalAccount();
您必须传递参数first,last,accountNumber,cardNumber,cardType。应该是这样的:
PersonalAccount class1 = new PersonalAccount("FirstName", "Last_Name", "123456", "123456789", "Visa");
阅读此内容:http://www.dummies.com/how-to/content/how-to-use-a-constructor-in-java.html