
本文旨在探讨java继承中一个常见的陷阱:子类方法试图修改父类成员变量时,错误地创建了局部变量而非更新现有状态。我们将通过具体代码示例,详细分析该问题产生的原因,并提供两种解决方案:优先使用父类提供的setter方法进行封装式修改,或在特定场景下通过将父类成员变量声明为protected来实现直接访问。
在Java面向对象编程中,继承是实现代码复用和构建层次结构的重要机制。然而,在子类中操作父类的状态(即成员变量)时,如果不注意细节,很容易遇到意想不到的行为。一个常见的误区是,子类方法在尝试修改父类继承的成员变量时,无意中创建了一个同名的局部变量,从而导致父类成员变量的值并未被实际更新。
考虑一个银行账户的继承体系,其中BaseAccount作为基类,DebitCard作为其子类。BaseAccount管理账户余额(currentAmount),并提供存款(deposit)和查询余额(balance)等功能。DebitCard类则在此基础上增加了取款(withdraw)功能。
初始的BaseAccount和DebitCard类定义如下:
public class BaseAccount {
private double opening; // 账户初始金额,实际中可能与currentAmount合并或用于记录初始操作
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方法,因为它们在子类中通过super()调用或被间接使用
public double getCurrentAmount() {
return currentAmount;
}
public void setCurrentAmount(double currentAmount) {
this.currentAmount = currentAmount;
}
public String opening(double opening) {
this.opening = opening;
this.currentAmount = currentAmount + opening; // 修改了父类的currentAmount
return "This account has been openend with " + this.opening;
}
public String deposit(double amount) {
this.currentAmount += amount; // 修改了父类的currentAmount
return "Depositing " + amount;
}
public String balance() {
return "Balance: " + currentAmount;
}
}
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;
}
}在main方法中进行测试时,我们会观察到DebitCard的withdraw方法执行后,后续的balance查询并未反映出取款操作。
立即学习“Java免费学习笔记(深入)”;
public class Inheritance {
public static void main(String[] args) {
DebitCard debit1 = new DebitCard(0, 0, 0);
System.out.println(debit1.opening(400)); // 账户开通400
System.out.println(debit1.deposit(33.33)); // 存款33.33
System.out.println(debit1.balance()); // 预期: 433.33, 实际: 433.33
System.out.println(debit1.withdraw(33.33)); // 取款33.33
// 打印结果显示取款后余额为400.0,但这是局部变量的值
System.out.println(debit1.balance()); // 预期: 400.0, 实际: 433.33
// 此时,balance方法依然返回433.33,说明父类的currentAmount未被修改
}
}问题根源:DebitCard类中的withdraw方法内部的这一行代码是问题的关键: double currentAmount = getCurrentAmount() - amount;
这行代码并没有修改BaseAccount类中继承下来的currentAmount成员变量。相反,它在withdraw方法的局部作用域内声明了一个新的局部变量,也叫做currentAmount。这个局部变量“遮蔽”了父类的成员变量。因此,getCurrentAmount() - amount的计算结果被赋给了这个局部变量,而父类的currentAmount成员变量保持不变。当withdraw方法执行完毕,这个局部变量即被销毁,其值不会对任何外部状态产生影响。随后的debit1.balance()调用会访问父类的currentAmount成员变量,其值仍然是取款前的433.33。
要正确地在子类中修改父类的成员变量,需要明确地操作父类实例的成员变量,而不是创建局部变量。通常有两种方法可以实现这一点。
这是最符合面向对象封装原则的方法。如果父类提供了公共或受保护的setter方法来修改其私有成员变量,子类应该通过调用这些setter方法来更新状态。
public class DebitCard extends BaseAccount {
public DebitCard(double opening, double currentAmount, double amount) {
super(opening, currentAmount, amount);
}
public String withdraw(double amount) {
// 正确做法:通过父类的getter获取当前值,计算新值,然后通过父类的setter更新父类的成员变量
double newAmount = getCurrentAmount() - amount;
setCurrentAmount(newAmount); // 调用父类的setter方法更新currentAmount
return amount + " have been retired. \nBalance: " + newAmount;
}
}优点:
如果父类设计者希望子类能够直接访问和修改某些成员变量,可以将这些成员变量的访问修饰符从private改为protected。protected成员可以在其自身类、同一包内的其他类以及所有子类中访问。
// BaseAccount.java
public class BaseAccount {
// ... 其他字段和方法 ...
protected double currentAmount = 0.0; // 将private改为protected
// ... 省略getter和setter,但通常仍会保留以提供更受控的访问 ...
}
// DebitCard.java
public class DebitCard extends BaseAccount {
// ... 构造函数 ...
public String withdraw(double amount) {
// 直接修改父类的protected成员变量
this.currentAmount -= amount; // 或 currentAmount = this.currentAmount - amount;
return amount + " have been retired. \nBalance: " + this.currentAmount;
}
}优点:
缺点:
通常情况下,方案一(使用Setter方法)是更推荐的做法,因为它更好地遵循了封装原则,使得类结构更加健壮和易于维护。只有在父类明确设计为允许子类直接操作某些内部状态时,才应考虑使用protected修饰符。
在Java继承体系中,子类对父类成员变量的修改必须谨慎。核心原则是区分局部变量和成员变量的作用域。当子类需要修改父类继承的私有成员变量时,应通过调用父类提供的公共或受保护的setter方法来实现。如果父类成员变量被声明为protected,子类可以直接访问和修改,但这会牺牲一定的封装性。选择合适的策略,能够有效避免状态管理上的错误,并构建出更健壮、可维护的面向对象系统。
以上就是Java继承中子类正确管理父类状态的实践的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号