
本文旨在解决Java继承中子类方法无法正确更新父类属性值的问题。通过分析示例代码,详细解释了局部变量与类成员变量的区别,并提供了修改方案,确保子类方法能够正确修改和保持父类的状态。
在Java的继承关系中,子类可以继承父类的属性和方法。然而,在子类方法中修改父类属性时,可能会遇到无法正确保持修改后的值的问题。这通常是由于对局部变量和类成员变量的理解不够透彻造成的。下面我们将通过一个具体的例子来详细分析这个问题,并提供解决方案。
考虑以下场景:我们有一个BaseAccount(基础账户)类和一个DebitCard(借记卡)类,DebitCard类继承自BaseAccount类。我们希望在DebitCard类的withdraw(取款)方法中修改账户余额,但发现余额并没有被正确更新。
以下是示例代码:
立即学习“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;
}
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 = currentAmount + opening;
return "This account has been opened with " + this.opening;
}
public String deposit(double amount) {
this.currentAmount += amount;
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) {
double currentAmount = getCurrentAmount() - amount;
return amount + " have been retired. \nBalance: " + currentAmount;
}
}
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());
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());
System.out.println(debit1.withdraw(33.33));
System.out.println(debit1.balance());
}
}运行上述代码,会发现DebitCard的withdraw方法并没有正确更新账户余额。这是因为在withdraw方法中,我们创建了一个新的局部变量currentAmount,而不是修改父类的currentAmount属性。
public String withdraw(double amount) {
double currentAmount = getCurrentAmount() - amount; // 创建了一个局部变量
return amount + " have been retired. \nBalance: " + currentAmount;
}要解决这个问题,我们需要使用setCurrentAmount方法来修改父类的currentAmount属性。修改后的withdraw方法如下:
public String withdraw(double amount) {
double newCurrentAmount = getCurrentAmount() - amount;
setCurrentAmount(newCurrentAmount);
return amount + " have been retired. \nBalance: " + newCurrentAmount;
}在这个修改后的版本中,我们首先计算新的余额newCurrentAmount,然后使用setCurrentAmount方法将这个新值设置到父类的currentAmount属性中。这样,当我们再次调用balance方法时,就会得到正确的余额。
完整的DebitCard类代码如下:
public class DebitCard extends BaseAccount {
public DebitCard(double opening, double currentAmount, double amount) {
super(opening, currentAmount, amount);
}
public String withdraw(double amount) {
double newCurrentAmount = getCurrentAmount() - amount;
setCurrentAmount(newCurrentAmount);
return amount + " have been retired. \nBalance: " + newCurrentAmount;
}
}在Java继承中,子类方法修改父类属性时,需要注意区分局部变量和成员变量。通过使用getter和setter方法,可以确保子类能够正确地访问和修改父类的属性,从而实现预期的功能。希望本文能够帮助你解决类似的问题,并加深对Java继承的理解。
以上就是Java 继承中子类函数值保持问题详解的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号