繁体   English   中英

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

[英]How to use a static variable from a class to update a non-static instance variable in another class?

您好,我正在做这项作业,几乎完成了,但是一件事做得不好。 我创建了这两个类“ SavingAccount”和“ TimeAccount”,它们是“ Account”的子类,它们具有不同的兴趣计算类型,包括当前时间和上次更新帐户的时间。 我有一个时间变量,其建模为月份,并且在我的Test类中声明为零。 每当我存入,提取或转移资金时,都应该更新帐户lastTimeUpdated变量,使其等于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());


    }


}

在输出中,它说他们的上次更新时间仍然是1,尽管我多次调用click()方法并在其中存了一些钱。

这是我的存款方法,应该将其lastUpdated变量更改为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;
    }

}

public class TimeAccount extends Account {

    private static int number = 1;
    private final double interestRate = 0.1;

    public TimeAccount(Customer c, Bank b){
        super(c, b, 1, number);
        number++;   
    }

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

}

我知道这很长,但是我试图明确指出问题出在哪里,而我的另一个问题被标记为“重复”,但我找不到答案。

因此,我的程序不会更新lastUpdated时间,这就是为什么兴趣不起作用的原因。 提前致谢。

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

protected int currentTime = Test.month;

Test.month currentTime的值设置为创建Account时的Test.month 如果要更改,则必须对其进行更新。

看来您只是在初始化currentTime时才设置它,因此一旦创建了Account对象,它的currentTime就固定了,并且不会随着Test.month更改而更新。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM