无法对非静态字段进行静态引用

9o685dep  于 2021-06-30  发布在  Java
关注(0)|答案(6)|浏览(479)

如果这段代码格式不正确,我会提前道歉,尝试粘贴而不是重新键入每一行。如果它不正确,有人能告诉我一个简单的方法粘贴多行代码一次?
我的主要问题是,我不断收到一条错误消息,指出: Cannot make a static reference to the non-static field balance. 我尝试过使方法成为静态的,没有结果,并通过从头中删除“static”使main方法成为非静态的,但是我得到了以下消息: java.lang.NoSuchMethodError: main Exception in thread "main" 有人有什么想法吗?感谢您的帮助。

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;
    }   
}
hsgswve4

hsgswve41#

写下:

private static double balance = 0;

你也可以这样写:

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

a8jjtwal2#

如果你想的话,你可以保持你的取款和存款方法是静态的,但是你必须像下面的代码那样编写它。sb=期初余额,eb=期末余额。

Account account = new Account(1122, 20000, 4.5);

    double sB = Account.withdraw(account.getBalance(), 2500);
    double eB = Account.deposit(sB, 3000);
    System.out.println("Balance is " + eB);
    System.out.println("Monthly interest is " + (account.getAnnualInterestRate()/12));
    account.setDateCreated(new Date());
    System.out.println("The account was created " + account.getDateCreated());
aoyhnmkz

aoyhnmkz3#

台词

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

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

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

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

并从调用中删除balance参数

nlejzf6q

nlejzf6q4#

您试图直接从静态方法访问非静态字段,这在java中是不合法的。balance是一个非静态字段,因此可以使用对象引用访问它,也可以使其成为静态字段。

093gszye

093gszye5#

静态电话提取和存款是你的问题。账户提取(余额2500);这行行不通,因为“balance”是account的一个示例变量。代码没有什么意义,取款/存款不会封装在account对象本身吗?所以撤军应该更像

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

当然,根据你的问题,你可以在这里做额外的验证,以防止负平衡等。

s4chpxco

s4chpxco6#

main 是静态方法。它不能引用 balance ,这是一个属性(非静态变量)。 balance 只有通过对象引用(例如 myAccount.balance 或者 yourAccount.balance ). 但是当它通过类(例如 Account.balance (那是谁的余额?)
我对你的代码做了一些修改,这样它就可以编译了。

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

以及:

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

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

相关问题