不能对非静态字段进行静态引用 [英] cannot make a static reference to the non-static field

查看:43
本文介绍了不能对非静态字段进行静态引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果此代码格式不正确,我会提前道歉,尝试粘贴而不是重新键入每一行.如果不正确,有人可以告诉我一次粘贴多行代码的简单方法吗?

I apologize ahead of time if this code isn't formatted correctly, trying to paste instead of retyping each line. If it isn't right, can someone tell me an easy way to paste multiple lines of code at once?

我的主要问题是我不断收到一条错误消息,指出:无法对非静态字段余额进行静态引用.

My main question is that I keep getting an error message stating: Cannot make a static reference to the non-static field balance.

我尝试使方法静态,但没有结果,并通过从标题中删除静态"使主方法非静态,但随后我收到消息:java.lang.NoSuchMethodError: main Exception在线程main"中

I have tried making the methods static, with no result, and making the main method non-static by removing "static" from the header, but then I get the message: java.lang.NoSuchMethodError: main Exception in thread "main"

有人有什么想法吗?任何帮助表示赞赏.

Does anyone have any ideas? Any help is appreciated.

public class Account {

    public static void main(String[] args) {
        Account account = new Account(1122, 20000, 4.5);

        account.withdraw(balance, 2500);
        account.deposit(balance, 3000);
        System.out.println("Balance is " + account.getBalance());
        System.out.println("Monthly interest is " + (account.getAnnualInterestRate()/12));
        System.out.println("The account was created " + account.getDateCreated());
    }

    private int id = 0;
    private double balance = 0;
    private double annualInterestRate = 0;
    public java.util.Date dateCreated;

    public Account() {
    }

    public Account(int id, double balance, double annualInterestRate) {
        this.id = id;
        this.balance = balance;
        this.annualInterestRate = annualInterestRate;
    }

    public void setId(int i) {
        id = i;
    }

    public int getID() {
        return id;
    }

    public void setBalance(double b){
        balance = b;
    }

    public double getBalance() {
        return balance;
    }

    public double getAnnualInterestRate() {
        return annualInterestRate;
    }

    public void setAnnualInterestRate(double interest) {
        annualInterestRate = interest;
    }

    public java.util.Date getDateCreated() {
        return this.dateCreated;
    }

    public void setDateCreated(java.util.Date dateCreated) {
        this.dateCreated = dateCreated;
    }

    public static double withdraw(double balance, double withdrawAmount) {
        double newBalance = balance - withdrawAmount;
        return newBalance;
    }

    public static double deposit(double balance, double depositAmount) {
        double newBalance = balance + depositAmount;
        return newBalance;
    }   
}

推荐答案

account.withdraw(balance, 2500);
account.deposit(balance, 3000);

您可能希望非静态取款和存款并让它修改余额

you might want to make withdraw and deposit non-static and let it modify the balance

public void withdraw(double withdrawAmount) {
    balance = balance - withdrawAmount;
}

public void deposit(double depositAmount) {
    balance = balance + depositAmount;
}   

并从调用中删除 balance 参数

and remove the balance parameter from the call

这篇关于不能对非静态字段进行静态引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆