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

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

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

问题分析

考虑以下代码,其中BaseAccount是父类,DebitCard是子类,DebitCard的withdraw方法旨在扣除账户余额。

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 属性不是同一个变量。因此,对这个局部变量的修改不会影响父类的属性值。

解决方案

要解决这个问题,需要使用父类的 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 方法。直接在子类中声明同名变量不会修改父类的属性,而是创建了一个新的局部变量。 理解这一点对于编写正确的继承关系代码至关重要。