我有一个名为“Account”的类:
import java.util.Date;
public class Account {
public int id = 0; //Declare default id as 0
public double balance = 0; //Declare default balance as 0
public double annualInterestRate = 0; //Declare default annual interest rate as 0
public Date dateCreated = new Date(); //Declare date
//No argument constructor for Account
public Account() {
id = 0;
balance = 0.0;
annualInterestRate = 0.0;
}
//Constructor that accepts ID, Balance, and Annual Interest Rate
public Account(int newID, double newBalance, double newAnnualInterestRate) {
id = newID;
balance = newBalance;
annualInterestRate = newAnnualInterestRate;
}
//Get ID
public int getId() {
return id;
}
//Set ID
public void setId(int id) {
this.id = id;
}
//Get Balance
public double getBalance() {
return balance;
}
//Set Balance
public void setBalance(double balance) {
this.balance = balance;
}
//Get Annual Interest Rate
public double getAnnualInterestRate() {
return annualInterestRate;
}
//Set Annual Interest Rate
public void setAnnualInterestRate(double annualInterestRate) {
this.annualInterestRate = annualInterestRate;
}
//Get Date Created
public Date getDateCreated() {
return dateCreated;
}
//Withdraw method
double withdraw(double amount) {
return balance -= amount;
}
//Deposit method
double deposit(double amount) {
return balance += amount;
}
//Interest rate method
double getMonthlyInterestRate() {
return (balance * annualInterestRate) / 12;
}
} //End Account class
然后我创建了两个不同的子类“PreferredCustomer”和“CommercialCustomer”。这两个类应该继承主“Account”类的所有方法(存款、取款、月利率和所有getters和setters)。与子类的唯一区别是它们有一个预先确定的利率。
public class PreferredCustomer extends Account {
public double annualInterestRate;
public PreferredCustomer() {
}
public PreferredCustomer(int id, double balance) {
super();
this.annualInterestRate = .04;
}
} //end PreferredCustomer Class
我感觉目前的设置方式不准确。测试时,withdraw 和 deposit 方法有效,但尽管输入了 20,000 美元的起始余额,它仍然将起始余额设置为 0 美元并且不计算利率。
我正在测试这个类:
public class TestingAccountClass {
public static void main(String[] args) {
//Create accounts
CommercialCustomer myCommercialCustomerAccount = new CommercialCustomer(1124,
20000.00);
//Invoking deposit method from account class
myCommercialCustomerAccount.deposit(3000.00);
//Display account balance, monthly interest, and date created
System.out.println("\n\n----Commercial Account---");
System.out.println("Account Created On: "
+ myCommercialCustomerAccount.getDateCreated());
System.out.printf("Balance: $%.2f", myCommercialCustomerAccount.getBalance());
System.out.printf("\nMonthly Interest: $%.2f"
,myCommercialCustomerAccount.getMonthlyInterestRate());
当以这种方式测试类时,存款方法有效,但帐户类中的任何其他方法(除了提款)似乎都无效。任何意见,将不胜感激。谢谢你!
最佳答案
你这样做:
CommercialCustomer myCommercialCustomerAccount = new CommercialCustomer(1124, 20000.00);
但是,
public PreferredCustomer(int id, double balance) {
super();
this.annualInterestRate = .04;
}
您无需对天平进行任何操作!
您或许可以将其更改为:
public PreferredCustomer(int id, double balance) {
super();
this.balance = balance;
this.annualInterestRate = .04;
}
但是您将向 balance
写入两次。
此外,拥有两个同名变量(基础变量与子变量)是个坏主意 -> annualInterestRate
。
编辑 ---------------------------------------- 编辑
我会推荐这样的东西:
public Account() {
this(0, 0d, 0d);
}
public Account(int id, double balance, double interestRate) {
this.id = id;
this.balance = balance;
this.annualInterestRate = interestRate;
}
public PreferredCustomer(int id, double balance) {
super(id, balance, 0.04d);
}
EDIT2 ---------------------------------------- EDIT2
这是错误的。您正在进行整数除法。
return (balance * annualInterestRate) / 12;
更改为:
return (balance * annualInterestRate) / 12d;
或者这个:
return (balance * annualInterestRate) / 12.0;
关于java - 子类不继承所有方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16178703/