简体   繁体   English

Java中的子类和超类

[英]subclass and superclass in Java

over here i've tried to make a subclass BasicAccount from the superclass BankAccount.在这里,我尝试从超类 BankAccount 创建一个子类 BasicAccount。 Whilst making a withdrawal method that will not withdraw more money than there currently is in the account.在制定一种取款方法时,该取款方式不会提取比当前帐户中更多的钱。

But i still don't understand why i can't access it in BasicAccount even though the variable is private in BankAccount.但是我仍然不明白为什么我不能在 BasicAccount 中访问它,即使该变量在 BankAccount 中是私有的。 Any idea on how to access the balance in my withdrawal method, by still keeping the balance field private?关于如何通过仍然保持余额字段私有来访问我的提款方法中的余额的任何想法?

/**
 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
    }
}

You need to call super.withdraw(double) from the subclass.您需要从子类中调用 super.withdraw(double) 。

Also

super.getBalance() -= amount;

This does nothing.这没有任何作用。 You cannot subtract and assign the value to a method, only to a variable.您不能将值减去和分配给方法,只能分配给变量。 This isn't logical.这不合逻辑。 Replace with what i said, super.withdraw(amount).替换为我所说的 super.withdraw(amount)。

Also

in BasicAccount#withdraw you have this.balance but you mean to say super.balance because this class, BasicAccount, doesn't define a balance class member, however the super class BankAccount does.在 BasicAccount#withdraw 你有 this.balance 但你的意思是说 super.balance 因为这个类 BasicAccount 没有定义 balance 类成员,但是类 BankAccount 定义了。

Also

        BasicAccount(double initialBalance) {

        }

You need to call super(initialBalance) because right now you're not giving calling the super constructor which assigns balance.您需要调用 super(initialBalance) ,因为现在您没有调用分配余额的超级构造函数。

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

Also (oof)还有(哦)

        public BankAccount() {
            balance = 0;
        }

Instead of creating a default constructor that does the same thing your other one does, either delete it since balance is 0 by default, or call the other constructor that has purpose.与其创建一个默认构造函数来执行与另一个构造函数相同的操作,不如删除它,因为默认情况下 balance 为 0,或者调用另一个有目的的构造函数。

        public BankAccount() {
            this(0);
        }

Now your initial balance constructor can do some useful edge case checks that the no-args constructor benefits from.现在,您的初始余额构造函数可以进行一些有用的边缘情况检查,无参数构造函数可以从中受益。

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

If you want to access a field of a superclass from a subclass you have to declare it protected instead of private.如果要从子类访问超类的字段,则必须将其声明为 protected 而不是 private。 You will then be able to access it with super.variableName .然后您就可以使用super.variableName访问它。

Also see what @Jason wrote, he pointed out other mistakes.另请参阅@Jason 写的内容,他指出了其他错误。

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

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