繁体   English   中英

Java中的子类和超类

[英]subclass and superclass in Java

在这里,我尝试从超类 BankAccount 创建一个子类 BasicAccount。 在制定一种取款方法时,该取款方式不会提取比当前帐户中更多的钱。

但是我仍然不明白为什么我不能在 BasicAccount 中访问它,即使该变量在 BankAccount 中是私有的。 关于如何通过仍然保持余额字段私有来访问我的提款方法中的余额的任何想法?

/**
 A bank account has a balance that can be changed by
 deposits and withdrawals.
 */
class BankAccount
{
    private double balance;

    /**
     Constructs a bank account with a zero balance.
     */
    public BankAccount()
    {
        balance = 0;
    }

    /**
     Constructs a bank account with a given balance.
     @param initialBalance the initial balance
     */
    public BankAccount(double initialBalance)
    {
        balance = initialBalance;
    }

    /**
     Deposits money into the bank account.
     @param amount the amount to deposit
     */
    public void deposit(double amount)
    {
        double newBalance = balance + amount;
        balance = newBalance;
    }

    /**
     Withdraws money from the bank account.
     @param amount the amount to withdraw
     */
    public void withdraw(double amount)
    {
        double newBalance = balance - amount;
        balance = newBalance;
    }

    /**
     Gets the current balance of the bank account.
     @return the current balance
     */
    public double getBalance()
    {
        return balance;
    }
}
    class BasicAccount extends BankAccount{


    BasicAccount(double initialBalance) {

    }


        public void withdraw(double amount) {
            if (amount > 0 && this.balance - amount >= 0) {
                super.getBalance() -= amount;
            } else if (amount < 0) {
                throw new IllegalArgumentException("Withdraw amount should be positive and greater than 0.");
            } else {
                System.out.println("Error: Withdraw amount exceeds available funds.");
            }
        }



}

class Main {
    public static void main(String args[]) {
        BankAccount account = new BasicAccount(100.00);
        double balance = account.getBalance(); //expected 100.00;

        account.withdraw(80.00);
        balance = account.getBalance(); //expected 20.00;

        account.withdraw(50.00);
        balance = account.getBalance(); //expected 20.00 because the amount to withdraw is larger than the balance
    }
}

您需要从子类中调用 super.withdraw(double) 。

super.getBalance() -= amount;

这没有任何作用。 您不能将值减去和分配给方法,只能分配给变量。 这不合逻辑。 替换为我所说的 super.withdraw(amount)。

在 BasicAccount#withdraw 你有 this.balance 但你的意思是说 super.balance 因为这个类 BasicAccount 没有定义 balance 类成员,但是类 BankAccount 定义了。

        BasicAccount(double initialBalance) {

        }

您需要调用 super(initialBalance) ,因为现在您没有调用分配余额的超级构造函数。

        BasicAccount(double initialBalance) {
            super(initialBalance);
        }

还有(哦)

        public BankAccount() {
            balance = 0;
        }

与其创建一个默认构造函数来执行与另一个构造函数相同的操作,不如删除它,因为默认情况下 balance 为 0,或者调用另一个有目的的构造函数。

        public BankAccount() {
            this(0);
        }

现在,您的初始余额构造函数可以进行一些有用的边缘情况检查,无参数构造函数可以从中受益。

    public BankAccount(double initialBalance) {
        if (initialBalance < 0) {
            throw new IllegalArgumentException("Initial balance cannot be below zero.");
        }
        balance = initialBalance;
    }

如果要从子类访问超类的字段,则必须将其声明为 protected 而不是 private。 然后您就可以使用super.variableName访问它。

另请参阅@Jason 写的内容,他指出了其他错误。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM