
本文探讨了java继承中子类修改父类私有字段的常见误区及解决方案。通过分析一个银行账户示例,揭示了在子类方法中创建局部变量而非更新父类实例字段的问题。教程强调了利用父类提供的公共setter方法来安全、有效地管理和修改继承状态的重要性,确保数据一致性,并提供了详细的代码示例和最佳实践。
在Java面向对象编程中,继承允许子类复用父类的属性和行为。然而,当子类需要修改父类中定义的私有(private)字段时,需要遵循特定的规则。直接访问或修改私有字段是不允许的,子类必须通过父类提供的公共(public)或受保护(protected)的访问器方法(即getter和setter)来进行操作。
考虑一个银行账户系统,其中BaseAccount作为基类,DebitCard作为其子类。BaseAccount包含账户余额currentAmount等信息。DebitCard类需要实现取款(withdraw)功能,这涉及到修改继承自BaseAccount的currentAmount。
以下是最初的代码结构,其中DebitCard的withdraw方法未能正确更新账户余额:
// BaseAccount.java
public class BaseAccount {
private double opening;
private double currentAmount = 0.0; // 账户当前余额
private double amount; // 未使用的字段,可考虑移除
public BaseAccount(double opening, double currentAmount, double amount) {
this.opening = opening;
this.currentAmount = currentAmount;
this.amount = amount;
}
// Getter和Setter方法
public double getOpening() { return opening; }
public void setOpening(double opening) { this.opening = opening; }
public double getCurrentAmount() { return currentAmount; }
public void setCurrentAmount(double currentAmount) { this.currentAmount = currentAmount; }
public double getAmount() { return amount; }
public void setAmount(double amount) { this.amount = amount; }
// 业务方法
public String opening(double opening) {
this.opening = opening;
this.currentAmount += opening; // 更新currentAmount
return "This account has been opened with " + this.opening;
}
public String deposit(double amount) {
this.currentAmount += amount; // 更新currentAmount
return "Depositing " + amount;
}
public String balance() {
return "Balance: " + currentAmount;
}
}
// DebitCard.java
public class DebitCard extends BaseAccount {
public DebitCard(double opening, double currentAmount, double amount) {
super(opening, currentAmount, amount);
}
public String withdraw(double amount) {
// 问题所在:这里创建了一个局部变量currentAmount
double currentAmount = getCurrentAmount() - amount;
return amount + " have been retired. \nBalance: " + currentAmount;
}
}
// Inheritance.java (主测试类)
public class Inheritance {
public static void main(String[] args) {
BaseAccount base1 = new BaseAccount(0,0,0);
System.out.println(base1.opening(500));
System.out.println(base1.deposit(22.22));
System.out.println(base1.balance()); // 预期输出:Balance: 522.22
DebitCard debit1 = new DebitCard(0,0,0);
System.out.println(debit1.opening(400));
System.out.println(debit1.deposit(33.33));
System.out.println(debit1.balance()); // 预期输出:Balance: 433.33
System.out.println(debit1.withdraw(33.33)); // 预期输出:33.33 have been retired. \nBalance: 400.0
System.out.println(debit1.balance()); // 实际输出:Balance: 433.33,而不是400.0
}
}运行上述代码,会发现DebitCard对象在执行withdraw操作后,其后续的balance()方法调用仍然返回取款前的余额。这表明withdraw方法并未成功修改BaseAccount中实际的currentAmount字段。
立即学习“Java免费学习笔记(深入)”;
问题出在DebitCard类的withdraw方法中的这一行: double currentAmount = getCurrentAmount() - amount;
在这里,double currentAmount声明了一个新的局部变量,其名称恰好与父类中的实例字段currentAmount相同。当这行代码执行时,它计算了取款后的余额,并将结果赋给了这个局部变量。父类中的实例字段currentAmount从未被修改。因此,当再次调用debit1.balance()时,它仍然读取的是父类中未被更新的currentAmount值。
由于BaseAccount中的currentAmount字段是private的,子类DebitCard无法直接访问或修改它。正确的做法是利用BaseAccount提供的public方法setCurrentAmount()来更新该字段。
为了正确地更新父类的currentAmount字段,DebitCard的withdraw方法应该调用BaseAccount中定义的setCurrentAmount()方法。
修改后的DebitCard类如下:
// DebitCard.java (修正版)
public class DebitCard extends BaseAccount {
public DebitCard(double opening, double currentAmount, double amount) {
super(opening, currentAmount, amount);
}
public String withdraw(double amount) {
// 正确做法:获取当前余额,计算新余额,然后通过setter方法更新父类的实例字段
double newAmount = getCurrentAmount() - amount;
setCurrentAmount(newAmount); // 使用父类的setter方法更新currentAmount字段
return amount + " have been retired. \nBalance: " + newAmount;
}
}使用修正后的DebitCard类再次运行main方法,输出将是正确的:
This account has been opened with 500.0 Depositing 22.22 Balance: 522.22 This account has been opened with 400.0 Depositing 33.33 Balance: 433.33 33.33 have been retired. Balance: 400.0 Balance: 400.0 // 正确更新后的余额
现在,debit1.balance()在withdraw之后正确地反映了取款后的余额。
在Java继承体系中,子类对父类私有字段的修改必须通过父类提供的公共setter方法进行。直接在子类方法中创建同名的局部变量会导致对局部变量的操作,而无法影响父类的实例字段,从而造成数据不一致。理解并正确运用封装原则和访问器方法是编写健壮、可维护的Java代码的关键。同时,审视并优化类中的字段设计,移除不必要的字段,有助于提高代码质量。
以上就是Java继承中子类状态管理:正确更新父类私有字段的实践的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号