0

0

正确处理Java继承中子类方法对父类属性的修改

霞舞

霞舞

发布时间:2025-10-30 18:32:17

|

977人浏览过

|

来源于php中文网

原创

正确处理java继承中子类方法对父类属性的修改

本文旨在解决Java继承中子类方法修改父类属性时遇到的值无法保持的问题。通过分析问题代码,明确原因在于子类方法中创建了局部变量而非修改父类属性,并提供使用`setter`方法正确更新父类属性的解决方案,从而保证子类方法对父类状态的修改能够生效。

在Java的继承关系中,子类可以继承父类的属性和方法。然而,当子类的方法需要修改父类的属性时,如果不注意处理,可能会出现修改无效的情况。本文将通过一个具体的例子,分析这种问题的原因,并提供正确的解决方案。

问题分析

考虑以下代码,其中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 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方法似乎没有生效,最后的账户余额仍然是存款后的余额,而不是扣除后的余额。

问题出在DebitCard的withdraw方法中:

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

这行代码 double currentAmount = getCurrentAmount() - amount; 声明了一个局部变量 currentAmount,它与父类BaseAccount中的 currentAmount 属性不是同一个变量。因此,对这个局部变量的修改不会影响父类的属性值。

如此AI员工
如此AI员工

国内首个全链路营销获客AI Agent

下载

解决方案

要解决这个问题,需要使用父类的 setCurrentAmount 方法来更新父类的 currentAmount 属性。修改后的 withdraw 方法如下:

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

在这个修改后的版本中,首先计算新的余额,然后通过 setCurrentAmount(newCurrentAmount) 方法将新的余额设置回父类的 currentAmount 属性。

完整代码示例

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 newCurrentAmount = getCurrentAmount() - amount;
        setCurrentAmount(newCurrentAmount);
        return amount + " have been retired. \nBalance: " + newCurrentAmount;
    }
}

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

总结

在Java继承中,当子类需要修改父类的私有属性时,务必使用父类提供的 getter 和 setter 方法。直接在子类中声明同名变量不会修改父类的属性,而是创建了一个新的局部变量。 理解这一点对于编写正确的继承关系代码至关重要。

相关专题

更多
java
java

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

842

2023.06.15

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

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

742

2023.07.05

java自学难吗
java自学难吗

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

739

2023.07.31

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

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

397

2023.08.01

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

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

399

2023.08.02

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

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

446

2023.08.02

java有什么用
java有什么用

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

431

2023.08.02

java在线网站
java在线网站

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

16926

2023.08.03

Golang 性能分析与pprof调优实战
Golang 性能分析与pprof调优实战

本专题系统讲解 Golang 应用的性能分析与调优方法,重点覆盖 pprof 的使用方式,包括 CPU、内存、阻塞与 goroutine 分析,火焰图解读,常见性能瓶颈定位思路,以及在真实项目中进行针对性优化的实践技巧。通过案例讲解,帮助开发者掌握 用数据驱动的方式持续提升 Go 程序性能与稳定性。

0

2026.01.22

热门下载

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

精品课程

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

共23课时 | 2.7万人学习

C# 教程
C# 教程

共94课时 | 7.2万人学习

Java 教程
Java 教程

共578课时 | 49万人学习

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

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