问题描述
你好,我有 3 个类 Account ,InvestmentAccount,Branch
我的 InvestmentAccount 类继承了 Account Class ,我的 Branch 类有一个 Account 数组和一些方法,现在除了使用关键字“instanceof”的方法外,一切似乎都有效 我想要做的是,如果此帐户是 InvestmentAccount,则获取其投资并存储它,然后在该方法获取所有投资后返回它 发生的事情是它总是返回 0.0,这很奇怪,就像 if 语句永远不会为真
这是方法:
public double totalInvestment() {
double investmentsSum = 0;
for (int i = 0; i < howMany; i++) {
if (arr[i] instanceof InvestmentAccount)
investmentsSum += ((InvestmentAccount)arr[i]).getInvestment();
}
return investmentsSum;
}
这是我的帐户类:
public class Account {
protected long accountNo;
protected long customerId;
protected String customerName;
protected double balance ;
public Account(long accountNo,long customerId,String customerName,double balance) {
this.accountNo = accountNo;
this.customerId = customerId;
this.customerName = new String(customerName);
this.balance = balance;
}
public Account(Account other) {
this.accountNo = other.accountNo;
this.customerId = other.customerId;
this.customerName = other.customerName;
this.balance = other.balance;
}
public long getAccountNo() {
return accountNo;
}
public long getCustomerId() {
return customerId;
}
public String getCustomerName() {
return customerName;
}
public double getBalance() {
return balance;
}
public void setAccountNo(long accountNo) {
this.accountNo = accountNo;
}
public void setCustomerId(long customerId) {
this.customerId = customerId;
}
public void setCustomerName(String customerName) {
this.customerName = customerName;
}
public void setBalance(double balance) {
this.balance = balance;
}
public boolean equals(Account a){
if(a.getAccountNo() == this.accountNo)
return true;
return false;
}
@Override
public String toString() {
return "Account{" +
"accountNo=" + accountNo +
",customerId=" + customerId +
",customerName='" + customerName + '\'' +
",balance=" + balance +
'}';
}
}
这是我的 InvestmentAccount 类:
public class InvestmentAccount extends Account {
private double investment;
private Date start;
private Date end;
private double rate;
public InvestmentAccount(long accountNo,double balance,double investment,Date start,Date end,double rate) {
super(accountNo,customerId,customerName,balance);
super.accountNo = accountNo;
this.investment = investment;
this.start = start;
this.end = end;
this.rate = rate;
}
public double getInvestment() {
return investment;
}
public Date getEnd() {
return end;
}
public Date getStart() {
return start;
}
public double getRate() {
return rate;
}
public void setEnd(Date end) {
this.end = end;
}
public void setInvestment(double investment) {
this.investment = investment;
}
public void setRate(double rate) {
this.rate = rate;
}
public void setStart(Date start) {
this.start = start;
}
@Override
public String toString() {
return "InvestmentAccount{" +
"investment=" + investment +
",start=" + start +
",end=" + end +
",rate=" + rate +
"} " + super.toString();
}
}
欢迎任何建议或帮助,提前致谢
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)