gpt4 book ai didi

java - Java中的子类和父类(super class)

转载 作者:行者123 更新时间:2023-12-02 08:54:13 26 4
gpt4 key购买 nike

在这里,我尝试从父类(super class) BankAccount 创建一个子类 BasicAccount。同时制定的提款方法不会提取比帐户中当前金额更多的资金。

但我仍然不明白为什么我无法在 BasicAccount 中访问它,即使该变量在 BankAccount 中是私有(private)的。您知道如何通过仍将余额字段保持私有(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
}
}

最佳答案

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

还有

super.getBalance() -= amount;

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

还有

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

还有

        BasicAccount(double initialBalance) {

}

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

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

还有(oof)

        public BankAccount() {
balance = 0;
}

不要创建一个与其他构造函数执行相同操作的默认构造函数,而是删除它(因为默认情况下余额为 0),或者调用其他具有目的的构造函数。

        public BankAccount() {
this(0);
}

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

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

关于java - Java中的子类和父类(super class),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60590288/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com