Java 如何使用一个类中的静态变量来更新另一个类中的非静态实例变量?

Java 如何使用一个类中的静态变量来更新另一个类中的非静态实例变量?,java,variables,static,Java,Variables,Static,你好,我正在做作业,作业差不多完成了,但有一件事做得不好。我创建了这两个类“SavingAccount”和“TimeAccount”,它们是“Account”的子类,它们有不同类型的利息计算,包括当前时间和帐户上次更新的时间。我有一个时间变量,它被建模为月份,在我的测试类中被声明为零。无论何时存款、取款或转账,它都应该更新accounts LastTimeUpdate变量,并使其等于currentMonth。这是我的测试课: public class Test { public sta

你好,我正在做作业,作业差不多完成了,但有一件事做得不好。我创建了这两个类“SavingAccount”和“TimeAccount”,它们是“Account”的子类,它们有不同类型的利息计算,包括当前时间和帐户上次更新的时间。我有一个时间变量,它被建模为月份,在我的测试类中被声明为零。无论何时存款、取款或转账,它都应该更新accounts LastTimeUpdate变量,并使其等于currentMonth。这是我的测试课:

public class Test {

    public static int month = 0;

    static void click(){
        month++;
        System.out.println("Current month is " + month);
    }

    static void click(int x){
        month += x;
        System.out.println("Current month is " + month);
    }


    public static void main(String[] args) {

        Customer ali = new Customer("Ali");
        Customer veli = new Customer("Veli");
        Customer elif = new Customer("Elif");

        click();

        Account savingAccount1 = new SavingAccount(ali, garanti, 3);
        Account timeAccount1 = new TimeAccount(elif, akbank);

        click();

        savingAccount1.deposit(500);
        timeAccount1.deposit(400);

        click(5);

        System.out.println(savingAccount1.getLastUpdate());
        System.out.println(timeAccount1.getLastUpdate());


    }


}
虽然我调用了click()方法好几次,并在其中存入了一些钱,但在输出中,它显示他们最后一次更新的时间仍然是1

这是我的存款方法,它应该将其上次更新的变量更改为currentTime,但它没有

public abstract class Account {

    protected int currentTime = Test.month;
    protected int timeUpdated;

    public abstract double getBalance();

    public void deposit(double amount){
        balance = this.getBalance();
        balance += amount;
        timeUpdated = currentTime;
    }
}
这是每个子类的getBalance()方法,因为它们有不同类型的兴趣计算:

public class SavingAccount extends Account {

    private final int term;
    private static int number = 1;
    private final double interestRate = 0.2;

    public SavingAccount(Customer c, Bank b, int t){
        super(c, b, 0, number);
        term = t;
        number++;   
    }

    @Override
    public double getBalance() {
        double a = (1+term*interestRate/12);
        double b = (currentTime-timeUpdated)/term;
        balance = balance*Math.pow(a,b);
        return balance;
    }

}

我知道这很长,但我试图弄清楚问题在哪里,我的另一个问题标记为“重复”,但我找不到答案


所以我的程序不会更新上次更新的时间,这就是为什么兴趣不起作用。提前感谢。

分配静态变量不会持续更新

protected int currentTime = Test.month;

这将
currentTime
的值设置为创建
帐户时的
Test.month
值。如果您想更改它,您必须更新它。

初始化它时,您似乎只设置了
currentTime
,因此,创建帐户对象后,它的currentTime是固定的,不会随着
Test.month
的更改而更新。

您只分配了一次currentTime(在实例化时)。更新Test.month时,它不会自动更新。您需要一种机制来更新Account的每个实例中的时间,并在您调用click的每个位置调用它。的确,Test.month成员并不是特别有用。

在您上面粘贴的代码中,没有任何地方包含getLastUpdate()的实现……是的,我很抱歉,我清除了大部分代码以缩短它,所以我想我也删除了它。那么我是否应该在click()中添加另一个方法来更新Account类中的currentTime?是的,你应该。需要注意的是,您需要更新实例化的每个Account实例。我在click()方法中添加了另一个方法,该方法更新Account类中的currentTime。现在它可以工作了。感谢您接受其中一个作为您问题的正式答案。
protected int currentTime = Test.month;