java - 了解继承和不同的类

标签 java class inheritance

我正在搞继承,并且对如何做一些不同的事情感到困惑。这是我所拥有的:

查看 Account 类并在不同的 Bank 类中编写一个 main 方法,以简要实验 Account 类的一些实例。

  1. 使用 Account 类作为基类,编写两个名为 SavingsAccount 和 CheckingAccount 的派生类。 SavingsAccount 对象除了具有 Account 对象的属性外,还应具有利息变量和为帐户添加利息的方法。 CheckingAccount 对象除了 Account 对象的实例变量之外,还应该有一个 overdraw 限额变量。确保您已根据需要在两个派生类中重写了 Account 类的方法。

  2. 现在创建一个 Bank 类,该类的对象包含 Account 对象的数组。数组中的帐户可以是 Account 类、SavingsAccount 类或 CheckingAccount 类的实例。创建一些测试帐户(每种类型各一些)。

  3. 在银行类中编写一个更新方法。它遍历每个帐户,并通过以下方式更新它: 储蓄帐户增加利息(通过您已经编写的方法);如果支票账户处于 overdraw 状态,则会收到一封信。

public class Account {

    private double balance; 
    private int acctNum; 

    public Account (int num)
    { 
        balance = 0.0;
        acctNum = num; 
    } 
    public void deposit (double amt)
    { 
        if (amt >0)
            balance +=amt;
        else 
            System.out.println("Account.deposit(...): "
                    +"cannot deposit negative amount.");
    }
    public void withdraw (double amt)
    {
        if (amt>0)
            balance -=amt; 
        else 
            System.err.println("Account.withdraw(...): "
                    +"cannot withdraw negative amount.");

    }
    public double getBalance() 
    { 
        return balance; 
    }
    public double getAccountNumber()
    {
        return acctNum;
    }
    public String toString()
    {
      return "Acc " + acctNum + ": " + "balance = "+ balance;   
    }
    public final void print()
    {
           System.out.println( toString()); 
    }

}

现在是储蓄账户

public class SavingsAccount extends Account { 
    private double interest;

    public SavingsAccount(int acctNum, double interest) {
        super(acctNum);
        this.interest=interest;
    }

    public double getInterest() { 
        double x= getBalance() + getBalance()*interest;
        return x;

    // public void setInterest((interest))
    // this.interest=interest;
    }
    public void AddInterest (double interest) { 
        double x = super.getBalance() * interest; 
        super.deposit(x);
    }
    public String toString() {
        return super.toString()+" Interest : " + interest; 
    }
}

检查帐户

public class CheckingAccount extends Account {
    private double limit; 

    public CheckingAccount(int acctNum, double limit) {
        super(acctNum);
        this.limit=limit;
    }

    public double getLimit() {
        return this.limit;
    }

    public void setLimit(double limit) { 
        this.limit=limit; 
    }
    public void withdraw (double limit) { 
        if (limit <= this.limit)
            super.withdraw(limit);
        else { 
            System.out.println(" Sorry, Limit Exceeded" );
        }

    }


    public String toString() { 
        return super.toString() +"Limit :   "+limit; 
    }
}

银行类

public class Bank {

    public static void main(String[] args) {
        Account[] accounts = new Account[2];
        accounts[0] = new SavingsAccount(2, 0.25);
        accounts[1] = new CheckingAccount(23, 50);

        for(int i=0; i<accounts.length;i++) {
            if (accounts[0].equals(SavingsAccount)
                System.out.println(accounts[0].getInterest());
        }
    }

这是我注意到的问题。

  1. 在 SavingsAccount 中,我不确定如何让 setInterest() 工作。我尝试将其更改为 int,但这会更改 Account 类。我怎样才能让它正常工作?

  2. 在银行类中 - 我编写了 for 循环。它不起作用,我已经尝试过 if(accounts[i].equals 如此,但似乎无法正常工作。正确的方法是什么?

同样,我假设所有内容都来 self 的 SavingsAccount 类。

最佳答案

如果我明白你想在 for 循环中做什么,你需要使用 instanceof检查类,而不是 equals()

举个例子

Account[] accounts = new Account[2];
accounts[0] = new SavingsAccount(2, 0.25);
accounts[1] = new CheckingAccount(23, 50);

for(int i=0; i<accounts.length;i++) {
    if (accounts[i] instanceof SavingsAccount) {
        // You must cast an Account to use any of the descendant's methods
        SavingsAccount account = (SavingsAccount) accounts[i];
        System.out.println(account.getInterest());
    } else { // it's a CheckingAccount

    } 
}

除此之外,StackOverflow 不是一个让您倾倒作业要求的地方,因此您的问题过于宽泛,无法完整回答。

关于java - 了解继承和不同的类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35052243/

相关文章:

spring - 在没有类属性的xml中定义bean

java - 如何在Processing中继承私有(private)变量?

inheritance - 抽象类+子类字段条件的JPA查询

javascript - JS 继承示例 : too much recursion

java - 如何使用 OPENSHIFT_DATA_DIR 环境变量在 Openshift 应用程序中引用自定义文件

java - 创建逐步验证

java - 使用 Google map api 的演示 Android 应用程序崩溃

java - 使用 Selenium 测试 URL 重定向

java - 测试方法的返回类型是否为数字?

c++ - 在函数内访问调用成员函数的对象