【问题标题】:Subclass not inheriting all methods子类不继承所有方法
【发布时间】:2013-04-17 05:16:33
【问题描述】:

我有一个名为“帐户”的课程:

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”类的所有方法(存款、取款、月利率以及所有 getter 和 setter)。与子类的唯一区别是它们具有预先确定的利率。

public class PreferredCustomer extends Account {

    public double annualInterestRate;

    public PreferredCustomer() {
    }

    public PreferredCustomer(int id, double balance) {
    super();
    this.annualInterestRate = .04;
    }

} //end PreferredCustomer Class

我感觉我目前的设置方式不准确。测试时,提款和存款方法有效,但尽管输入了 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());

以这种方式测试类时,deposit 方法有效,但 account 类中的任何其他方法(除了提款)似乎都在工作。任何意见,将不胜感激。谢谢!

【问题讨论】:

  • PreferredCustomer 不应定义 annualInterestRate - 它继承帐户的 annualInterestRate。另外,PreferredCustomer 应该调用super(id, balance, .04)
  • 目前,如果我取出年利率代码,它仍然不会吐出正确的余额:(问题是,子类需要有自己的利率,而不是继承主类。这不可能吗?

标签: java class inheritance subclass super


【解决方案1】:

PreferredCustomer 中没有设置余额的机制;您忽略了 balance 构造函数参数。您没有直接分配 balance 实例变量,也没有调用超类构造函数。所以余额为0。

PreferredCustomer构造函数中,要么调用设置余额的超类构造函数,要么在构造函数本身中设置余额,或者调用setBalance

【讨论】:

    【解决方案2】:

    你这样做:

    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;
    

    【讨论】:

    • 如果这看起来有点琐碎,我很抱歉,大学生,所以我还在学习:) 我想我可能对如何构建子类有误解。目前,我已经将其设置为类似于我的教授给我的示例,但显然我还缺少更多内容:/
    • 这似乎主要解决了它。余额正在更新,但利息仍未计算。我想回到绘图板:)
    • 非常感谢,这非常有效。我现在知道我哪里出错了,谢谢。
    【解决方案3】:

    我认为问题出在此处:

    public PreferredCustomer(int id, double balance) {
        super();
        this.annualInterestRate = .04;
        }
    

    你不应该在 super() 调用中加入一些东西吗? (默认值)

    【讨论】:

      【解决方案4】:

      警告!

      首先,您永远不要公开您的非最终成员变量。我差点心脏病发作。

      当你输入时

      this.foo
      

      作用域继续向上爬上固有树,直到找到可访问的成员。所以 this-->super-->super.super-->super.super.super --...等

      我可以告诉你如何在语法上解决你的问题,或者告诉你如何做得更好。

      我选择后者。

      声明一个

      public abstract double getAnnualInterestRate(); 
      

      在你的基类中

      然后更改您的 getMonthlyInterestRate 实现(调用这个新方法)

         //Interest rate method
          double getMonthlyInterestRate() {
          return (balance * getAnnualInterestRate()) / 12;
         }
      

      在您的子类中简单地实现这个抽象方法并返回您的利率。

      这将允许您以多态方式改变速率,并使您的实现能够面向未来。函数可以做任何事情来产生它们的返回值,而成员变量只是一些数据而已

      并且,请把你的所有成员变量设为私有

      【讨论】:

      • 我当然很感激您的建议,尽管我认为我的知识有所欠缺。 “永远不要公开你的非最终成员变量”具体是什么意思?您是指我的 Account 类中的变量,例如 id、balance 等吗?当我最初创建 Account 类时,我将这些变量设为 Private,但我的印象是需要设置为 Public 才能被主类继承。我会立即解决这个问题:)
      • 还是有点不对劲。您仍然在这里执行整数除法。
      • 公开它们肯定会让子类可以访问它们。但是,这也将使它们为全世界所接受。成员变量代表对象的内部状态,并且只能由该对象通过它支持的机制(如 setter)或根本不(理想)来操作。控制方法中的变量访问限制了数据的操作位置和方式,并允许您执行代理、延迟加载验证等操作。请继续与我讨论。传统上,子类访问是通过声明一个成员受保护来完成的
      猜你喜欢
      • 1970-01-01
      • 2019-07-27
      • 2023-03-23
      • 1970-01-01
      • 2014-05-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-23
      相关资源
      最近更新 更多