0

0

Java 继承中子类方法值保持问题解决方案

碧海醫心

碧海醫心

发布时间:2025-10-30 19:58:00

|

522人浏览过

|

来源于php中文网

原创

java 继承中子类方法值保持问题解决方案

本文旨在解决Java继承中,子类方法修改父类属性后,数值未正确保持的问题。通过分析示例代码,解释了局部变量与成员变量的区别,并提供了修改方案,确保子类方法能够正确更新父类状态,从而实现期望的程序行为。

在Java的继承体系中,子类可以继承父类的属性和方法。然而,在子类方法中直接修改从父类继承来的属性时,可能会遇到数值未正确保持的问题。这通常是由于对变量作用域理解不当造成的。以下将通过一个账户管理的例子,详细讲解如何解决这个问题。

问题分析

考虑一个BaseAccount(基础账户)类和一个继承自BaseAccount的DebitCard(借记卡)类。BaseAccount类维护账户余额,DebitCard类提供取款功能。问题在于,当在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 openend 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方法中,以下代码是问题的关键:

double currentAmount = getCurrentAmount() - amount;

这行代码创建了一个局部变量 currentAmount,并将其初始化为取款后的余额。然而,它并没有修改BaseAccount类中声明的currentAmount 成员变量。因此,当调用balance()方法时,返回的仍然是未取款前的余额。

解决方案

刺鸟创客
刺鸟创客

一款专业高效稳定的AI内容创作平台

下载

要解决这个问题,需要使用setCurrentAmount()方法来更新BaseAccount类的currentAmount成员变量。修改后的withdraw方法如下:

public String withdraw(double amount) {
    double currentAmount = getCurrentAmount() - amount;
    setCurrentAmount(currentAmount);
    return amount + " have been retired. \nBalance: " + currentAmount;
}

这段代码首先计算取款后的余额,然后使用setCurrentAmount()方法将新的余额值赋给BaseAccount类的currentAmount成员变量。这样,balance()方法就能返回正确的余额。

完整代码

以下是修改后的完整代码:

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 openend 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;
        setCurrentAmount(currentAmount);
        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());
    }
}

运行结果

修改后的代码运行结果如下:

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

可以看到,取款后,账户余额已经正确更新。

注意事项

  • 务必区分局部变量和成员变量的作用域。
  • 在子类方法中修改父类属性时,应使用父类提供的getter和setter方法,以确保状态同步。
  • 考虑封装性,尽量避免直接访问父类的私有属性。

总结

通过本文的讲解,我们了解了在Java继承中,子类方法修改父类属性时可能遇到的问题,以及如何使用setter方法来正确更新父类状态。理解变量作用域和封装性是解决此类问题的关键。希望本文能帮助读者更好地理解和应用Java继承。

相关专题

更多
java
java

Java是一个通用术语,用于表示Java软件及其组件,包括“Java运行时环境 (JRE)”、“Java虚拟机 (JVM)”以及“插件”。php中文网还为大家带了Java相关下载资源、相关课程以及相关文章等内容,供大家免费下载使用。

845

2023.06.15

java正则表达式语法
java正则表达式语法

java正则表达式语法是一种模式匹配工具,它非常有用,可以在处理文本和字符串时快速地查找、替换、验证和提取特定的模式和数据。本专题提供java正则表达式语法的相关文章、下载和专题,供大家免费下载体验。

745

2023.07.05

java自学难吗
java自学难吗

Java自学并不难。Java语言相对于其他一些编程语言而言,有着较为简洁和易读的语法,本专题为大家提供java自学难吗相关的文章,大家可以免费体验。

740

2023.07.31

java配置jdk环境变量
java配置jdk环境变量

Java是一种广泛使用的高级编程语言,用于开发各种类型的应用程序。为了能够在计算机上正确运行和编译Java代码,需要正确配置Java Development Kit(JDK)环境变量。php中文网给大家带来了相关的教程以及文章,欢迎大家前来阅读学习。

397

2023.08.01

java保留两位小数
java保留两位小数

Java是一种广泛应用于编程领域的高级编程语言。在Java中,保留两位小数是指在进行数值计算或输出时,限制小数部分只有两位有效数字,并将多余的位数进行四舍五入或截取。php中文网给大家带来了相关的教程以及文章,欢迎大家前来阅读学习。

420

2023.08.02

java基本数据类型
java基本数据类型

java基本数据类型有:1、byte;2、short;3、int;4、long;5、float;6、double;7、char;8、boolean。本专题为大家提供java基本数据类型的相关的文章、下载、课程内容,供大家免费下载体验。

447

2023.08.02

java有什么用
java有什么用

java可以开发应用程序、移动应用、Web应用、企业级应用、嵌入式系统等方面。本专题为大家提供java有什么用的相关的文章、下载、课程内容,供大家免费下载体验。

431

2023.08.02

java在线网站
java在线网站

Java在线网站是指提供Java编程学习、实践和交流平台的网络服务。近年来,随着Java语言在软件开发领域的广泛应用,越来越多的人对Java编程感兴趣,并希望能够通过在线网站来学习和提高自己的Java编程技能。php中文网给大家带来了相关的视频、教程以及文章,欢迎大家前来学习阅读和下载。

16947

2023.08.03

c++ 根号
c++ 根号

本专题整合了c++根号相关教程,阅读专题下面的文章了解更多详细内容。

25

2026.01.23

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
Kotlin 教程
Kotlin 教程

共23课时 | 2.8万人学习

C# 教程
C# 教程

共94课时 | 7.5万人学习

Java 教程
Java 教程

共578课时 | 50.7万人学习

关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

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