3 Check帐户2 Cust的构造函数,没有透支,但不想透支的Cust正在得到一个



chequeaccount类扩展了帐户类。帐户类保存ID,名称和余额属性。Chequesaccount类具有透支限制,Amtoverdrawn和TransactionNo Plus Super(ID,name,balance(来自帐户。我在数组中创建了3个CHQACCOUNT对象,并打印了它们的详细信息。林戈(Ringo(选择了他的CHQ帐户中的透支设施,因此为他提供了独立的构造函数。但是,当我打印所有支票帐户的详细信息时,他是唯一获得透支设施的人。它掌握了我的头,请帮助

public class TestAccounts6
{
   private static ChequeAccount[] chqAccount = new ChequeAccount[5];
   private static int indexNo = 0;
   public static void main(String[] args)
   {
      ChequeAccount c1 = new ChequeAccount("S1111", "Paul", 1245.00, 0, 0, 0);
      ChequeAccount c2 = new ChequeAccount("S2222", "Ringo", 2500.00);
      ChequeAccount c3 = new ChequeAccount("S3333", "John", 1575.00, 0, 0, 0);
      chqAccount[0] = c1;
      chqAccount[1] = c2;
      chqAccount[2] = c3;
      indexNo = 3;
      System.out.printf("%-10s%-10s%-10s%-10s%-10s%-10s%n", "ID", "Name", "Balance",
                        "Overdraft", "Amount", "No of");
      System.out.printf("%-10s%-10s%-10s%-10s%-10s%-10s%n", "", "", "",
                        "Limit", "Overdrawn", "Transactionsn");
      for (int i = 0; i < indexNo; i++)
      {
         chqAccount[i].print();
      }
   }
}
public class ChequeAccount extends Account
{
   protected double overdraftLimit = 10000;
   protected double amtOverdrawn = 0;
   protected int transactionNo = 0;
   // constructor
   public ChequeAccount(String ID, String name, double balance,
                        double overdraftLimit, double amtOverdrawn,
                        int transactionNo)
   {
      super(ID, name, balance);
      this.overdraftLimit = overdraftLimit;
      this.amtOverdrawn = amtOverdrawn;
      this.transactionNo = transactionNo;
   }
   public ChequeAccount(String ID, String name, double balance)
   {
      super(ID, name, balance);
   }
   public void print()
   {
      System.out.printf("%-10s%-10s$%-9.2f$%-9.2f$%-9.2f%-10d%n", ID, name,
                        balance, overdraftLimit, amtOverdrawn, transactionNo);
   }
}
public class Account
{
   protected String ID;
   protected String name;
   protected double balance;
   // Constructor
   public Account(String ID, String name, double balance)
   {
      this.ID = ID;
      this.name = name;
      this.balance = balance;
   }

预计林戈不会得到透支设施,约翰和保罗会。与期望相反

它的行为正确。因为

  1. 您已经创建了3个chqaccount对象

chequeaccount c1 = new chequeaccount(" S1111"," Paul",1245.00,0,0,0, 0(;

chequeaccount c2 = new chequeaccount(" s2222"," ringo",2500.00(;

chequeaccount c3 = new chequeaccount(" S3333"," John",1575.00,0,0,0, 0(;

  1. 当您尝试打印该对象的值时。它正在打印以下方式。

ID名称余额透支金额
限制透支交易

S1111保罗$ 1245.00 $ 0.00 $ 0.00 0 S22222
林戈$ 2500.00 $ 10000.00 $ 0.00 0 S3333 JOHN
$ 1575.00 $ 0.00 $ 0.00 0

当我们观察上面并检查林戈的详细信息时。它的余额为:$ 10000.00,因为您尚未将任何值分配给ringo。但是在chequeaccount中如下。

  protected double overdraftLimit = 10000;
  protected double amtOverdrawn = 0;
  protected int transactionNo = 0;

它具有默认值。因此,在打印时,它正在采用默认值。

这是代码的预期行为。

public class ChequeAccount extends Account
{
   protected double overdraftLimit = 10000;
   protected double amtOverdrawn = 0;
   protected int transactionNo = 0;
   ...
}

您为这些变量分配了默认值。当您调用 ringo 的构造函数时,这些变量未更新,因此,将使用您分配的默认值。

使用 Paul John 您将新值(所有0(分配给这些变量,以便将它们打印出来。

ChequeAccount c1 = new ChequeAccount("S1111", "Paul", 1245.00, 10000, 0, 0);
ChequeAccount c2 = new ChequeAccount("S2222", "Ringo", 2500.00);
ChequeAccount c3 = new ChequeAccount("S3333", "John", 1575.00, 10000, 0, 0); 
public class ChequeAccount extends Account
{
   protected double overdraftLimit = 0;
   protected double amtOverdrawn = 0;
   protected int transactionNo = 0;
   ...
}

使用上述更改修改代码现在将显示出 Paul John 的透支限制在制作 ringo的限制0时。

最新更新