首页 > Java > java教程 > 正文

Java 继承中子类函数值保持问题详解

心靈之曲
发布: 2025-10-30 20:04:33
原创
660人浏览过

java 继承中子类函数值保持问题详解

本文旨在解决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属性。

AI建筑知识问答
AI建筑知识问答

用人工智能ChatGPT帮你解答所有建筑问题

AI建筑知识问答22
查看详情 AI建筑知识问答
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;
    }
}
登录后复制

注意事项

  1. 局部变量与成员变量的区别:在方法内部声明的变量是局部变量,其作用域仅限于该方法。而类的属性(成员变量)的作用域是整个类。
  2. 访问控制符:如果父类的属性是private的,子类不能直接访问,需要通过getter和setter方法来访问和修改。
  3. 代码可读性:为了提高代码的可读性,建议使用有意义的变量名,并添加适当的注释。

总结

在Java继承中,子类方法修改父类属性时,需要注意区分局部变量和成员变量。通过使用getter和setter方法,可以确保子类能够正确地访问和修改父类的属性,从而实现预期的功能。希望本文能够帮助你解决类似的问题,并加深对Java继承的理解。

以上就是Java 继承中子类函数值保持问题详解的详细内容,更多请关注php中文网其它相关文章!

最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号