3个支票帐户2个构造函数以进行无透支的保管,但不希望透支的保管人得到一个

时间:2019-07-05 04:00:47

标签: java inheritance constructor superclass

ChequeAccount类扩展了Account类。帐户类别包含ID,名称和余额属性。 ChequesAccount类具有Account的overdraftLimit,amtOverdrawn和transactionNo加上super(ID,名称,余额)。我在一个数组中创建了3个chqAccount对象,并打印了它们的详细信息。林戈已选择退出其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", "Transactions\n");
      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;
   }

预期的林戈将不会透支贷款,约翰和保罗会。与期望相反

2 个答案:

答案 0 :(得分:0)

它的行为正确。因为

  1. 您已创建3个chqAccount对象
  

ChequeAccount c1 =新的ChequeAccount(“ S1111”,“ Paul”,1245.00,0,0,   0);

     

ChequeAccount c2 =新的ChequeAccount(“ S2222”,“ Ringo”,2500.00);

     

ChequeAccount c3 =新的ChequeAccount(“ S3333”,“ John”,1575.00,0,0,   0);

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

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

     

S1111保罗$ 1245.00 $ 0.00 $ 0.00 0 S2222
  Ringo $ 2500.00 $ 10000.00 $ 0.00 0 S3333约翰
  $ 1575.00 $ 0.00 $ 0.00 0

当我们遵守上述规定并检查Ringo的详细信息时。它的余额为:$ 10000.00,因为您尚未为Ringo分配任何值。但是在ChequeAccount中,如下所示。

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

它具有默认值。这样在打印时它将采用默认值。

答案 1 :(得分: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