Account类被定义为一个银行帐户进行建模。一个帐户的属性账号,余额,年利率,和创建日期和方法来存取资金。
现在,创建两个子类,用于检查和储蓄账户。活期存款有一个透支额度(说与收取$ 25费$ 1,000),但储蓄账户不能透支。
编写创建帐户,SavingsAccount,和的CheckingAccount的对象,并调用其toString()方法的测试程序。
以上是下面的说明和是我的代码。我无法弄清楚如何子类调用到主账户类。我也想知道toString()方法如何应用,因为我无法获得,要么。我还不停的大部分我在我的代码中的注释,我是想不同的想法。
/*
//Calls both subclasses to the main. As well as a few other variables.
SavingsAccount savings = new SavingsAccount();
CheckingAccount checking = new CheckingAccount();
Account account;
double balance = 0.0;
double withdrawal = 0.0;
double deposit = 0.0;
System.out.println(checking);
CheckingAccount.getwithdrawal(10.50);
System.out.println(savings);
SavingsAccount.deposit(5.0);
System.out.println(account);
}
}
*/
package account;
public class Assignment5 {
SavingsAccount savings = new SavingsAccount();
CheckingAccount checking = new CheckingAccount();
Account account;
public static void main (String[] args) {
Account account = new Account(1122, 20000);
/* System.out.print("After Creation, " );
Print (account.getBalance());
Account.setAnnualInterestRate(4.5);
System.out.print("After Withdrawal of $2,500, " );
account.withdraw(2500);
Print (account.getBalance());
System.out.print("After Deposit of $3,000, " );
account.deposit(3000);
Print (account.getBalance());
System.out.println("Balance is " + account.getBalance());
System.out.println("Monthly interest is " +
account.getMonthlyInterest());
System.out.println("This account was created at " +
account.getDateCreated()); */
}
// Extra Print Method
public static void Print (double x){
System.out.printf("The current balance is "+" $ "+"%4.2f"+"%n",x);
}
}
class Account {
private int id;
double balance;
private static double annualInterestRate;
private java.util.Date dateCreated;
public Account() {
dateCreated = new java.util.Date();
}
public Account(int newId, double newBalance) {
id = newId;
balance = newBalance;
dateCreated = new java.util.Date();
}
public int getId() {
return this.id;
}
public double getBalance() {
return balance;
}
public static double getAnnualInterestRate() {
return annualInterestRate;
}
public void setId(int newId) {
id = newId;
}
public void setBalance(double newBalance) {
balance = newBalance;
}
public static void setAnnualInterestRate(double newAnnualInterestRate) {
annualInterestRate = newAnnualInterestRate;
}
public double getMonthlyInterest() {
return balance * (annualInterestRate / 1200);
}
public java.util.Date getDateCreated() {
return dateCreated;
}
public void withdraw(double amount) {
balance -= amount;
}
public void deposit(double amount) {
balance += amount;
}
}
package account;
public class CheckingAccount extends Account {
double overDraft = -1000;
public void checking(double i) {
//initializes double variable balance as 0.0
double balance = 0.0;
if (balance - i < overDraft){
System.out.println("Failure: Can't overdraft more than $1,000. A $25 +"
+ "overdraft fee will be issued to your account. ");
balance = balance - 25; }
else
balance = balance - i;
}
}
package account;
public class SavingsAccount extends Account{
double overdraftLimit = 0;
public void withdraw (double w) {
if (balance - w < overdraftLimit)
System.out.println("Insufficient Funds");
else
balance = balance - w;
}
}
我不知道你通过调用子类的意思,但对于toString方法签名
public String toString(){
}
既然一切都虚拟在Java中默认,然后只需在子类的方法来覆盖它。从我可以猜到,这似乎是这个问题要你调用toString方法的各种方法和返回所需的所有信息的完整字符串。
在另一方面,调用父类方法,即使子类覆盖它在Java中,语法是
super.function()
首先,你需要创建一个默认的构造函数储蓄存款和支票账户。然后,你需要创建一个重载的构造函数(假设你将被传递的唯一的额外的值到子类)
public savingsAccount() {
super();
}
//overloaded constructor
public savingsAccount(int newId, double newBalance, int anotherVariable ) {
super(newID, newBalance);
this.anotherVariable = anotherVariable; //this is unique to this specific class
}
至于的toString(),你需要在每个子类有它自己的。
String toString(){
String x = "Savings: " + totalSavings + ". And the fee's assessed are: " + fees";
return x;
}
这将允许你打电话savingsAccount.toString();
,它将打印信息。
你需要重写每一个类例如默认的toString方法,在Account类
@Override
public String toString()
{
String str = "Id: "+ getId() +
"\nBalance: " + getBalance()+
"\nAnnual Interest Rate: " + getAnnualInterestRate() +
"\n Monthly Interest: " + getMonthlyInterest() +
"\n Created on: " + getDateCreated();
return str;
}
然后,创建主类帐户对象
public static void main(String[] args) {
// TODO Auto-generated method stub
Account account = new Account(1122, 20000);
System.out.println(account); // invoke toString method in Account class
}
结果将是:编号:1122余额:20000.0年利率:0.0月息0.0创建于:周四02月18日十五时16分47秒的PST 2016
同样,你需要重写的子类的toString方法也。但是,你需要使用super关键字来超的toString连接:
public String toString()
{
String str=super.toString();// call superclass's toString()
str+="...";
return str;
}