Java中的子类和超类
在这里,我尝试从超类BankAccount生成一个子类BasicAccount。同时采取取款方式,取款金额不会超过当前账户中的金额 但是我仍然不明白为什么我不能在BasicAccount中访问它,即使变量在BankAccount中是私有的。你知道如何在我的取款方法中通过保持余额字段的私有性来访问余额吗Java中的子类和超类,java,oop,inheritance,Java,Oop,Inheritance,在这里,我尝试从超类BankAccount生成一个子类BasicAccount。同时采取取款方式,取款金额不会超过当前账户中的金额 但是我仍然不明白为什么我不能在BasicAccount中访问它,即使变量在BankAccount中是私有的。你知道如何在我的取款方法中通过保持余额字段的私有性来访问余额吗 /** A bank account has a balance that can be changed by deposits and withdrawals. */ class Bank
/**
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
}
}
/**
银行账户的余额可以通过以下方式更改:
存款和取款。
*/
类别银行帐户
{
私人双平衡;
/**
构造一个余额为零的银行帐户。
*/
公共银行账户()
{
余额=0;
}
/**
构造具有给定余额的银行帐户。
@param initialBalance初始余额
*/
公共银行账户(双倍初始余额)
{
平衡=初始平衡;
}
/**
把钱存入银行帐户。
@param amount要存入的金额
*/
公共作废保证金(双倍金额)
{
双新余额=余额+金额;
平衡=新平衡;
}
/**
从银行帐户取款。
@param amount要提取的金额
*/
公开作废取款(双倍金额)
{
双新余额=余额-金额;
平衡=新平衡;
}
/**
获取银行帐户的当前余额。
@返回当前余额
*/
公共双getBalance()
{
收益余额;
}
}
类BasicAccount扩展了BankAccount{
基本计数(双初始余额){
}
公开作废取款(双倍金额){
如果(金额>0&&this.balance-金额>=0){
super.getBalance()-=金额;
}否则,如果(金额<0){
抛出新的IllegalArgumentException(“提取金额应为正且大于0”);
}否则{
System.out.println(“错误:提取金额超过可用资金”);
}
}
}
班长{
公共静态void main(字符串参数[]){
银行账户=新的基本账户(100.00);
双倍余额=account.getBalance();//预期为100.00;
账户提款(80.00);
balance=account.getBalance();//预期为20.00;
账户提款(50.00);
balance=account.getBalance();//预期为20.00,因为要提取的金额大于余额
}
}
您需要从子类调用super.draw(double)
同时
super.getBalance()-=金额;
这没用。不能将值减去并赋给方法,只能赋给变量。这是不合逻辑的。替换为我说的,超级。取款(金额)
同时
在BasicAccount#draw中,您有这个.balance,但您的意思是说super.balance,因为这个类BasicAccount没有定义balance类成员,而super类BankAccount却定义了
同时
BasicAccount(双初始余额){
}
您需要调用super(initialBalance),因为现在您没有调用分配balance的超级构造函数
BasicAccount(双初始余额){
超级(初始余额);
}
也(oof)
公共银行账户(){
余额=0;
}
与其创建一个默认构造函数来执行与另一个相同的操作,不如删除它,因为默认情况下余额为0,或者调用另一个有用途的构造函数
公共银行账户(){
这(0);
}
现在,您的初始balance构造函数可以执行一些有用的边缘案例检查,无参数构造函数可以从中受益
public BankAccount(double initialBalance) {
if (initialBalance < 0) {
throw new IllegalArgumentException("Initial balance cannot be below zero.");
}
balance = initialBalance;
}
如果要从子类访问超类的字段,必须将其声明为protected而不是private。然后,您可以使用
super.variableName
访问它
也看看@Jason写了什么,他指出了其他错误