子类不继承所有方法

时间:2013-04-23 20:34:48

标签: java class inheritance subclass super

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

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”。这两个类应继承主“帐户”类的所有方法(存款,取款,月利率以及所有获取者和设定者)。与子类的唯一区别在于它们具有预先确定的利率。

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());

以这种方式测试课程时,存款方法有效,但帐户类别(除了撤销)之外的任何其他内容似乎都无效。任何意见,将不胜感激。谢谢!

4 个答案:

答案 0 :(得分:4)

你这样做:

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

另外,两个具有相同名称的变量(base vs child)是一个坏主意 - > 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;

答案 1 :(得分:1)

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

PreferredCustomer构造函数中,调用设置余额的超类构造函数,或者在构造函数本身中将其设置在那里,或者调用setBalance

答案 2 :(得分:0)

我认为问题出在这里:

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

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

答案 3 :(得分:0)

ALERT!

对于开始,你永远不要公开你的非最终成员变量。我几乎只是心脏病发作。

输入

时输入

this.foo

范围继续爬上不变的树,直到找到一个可访问的成员。所以 这个 - >超级 - > super.super - > super.super.super --...等

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

我选择后者。

宣布

public abstract double getAnnualInterestRate(); 

在您的基类

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

   //Interest rate method
    double getMonthlyInterestRate() {
    return (balance * getAnnualInterestRate()) / 12;
   }
你的子类中的

只需实现这个抽象方法并返回你的利率。

这将允许您以多态方式改变速率并使您的实现在未来证明。函数可以做任何事情来产生它们的返回值,其中成员变量只是一些数据,而不再是

并且,请将您的所有成员变量设为私有