不能对非静态字段进行静态引用
如果这段代码格式不正确,我会提前道歉,试图粘贴而不是重新input每一行。 如果不正确,有人可以告诉我一个简单的方法来粘贴多行代码吗?
我的主要问题是,我不断收到一个错误消息,指出: Cannot make a static reference to the non-static field balance.
我试图使方法静态,没有结果,并通过从头去除“静态”,使主要方法非静态,但后来我得到的消息: 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; } }
线
account.withdraw(balance, 2500); account.deposit(balance, 3000);
你可能想要退出和存款非静态,让它修改余额
public void withdraw(double withdrawAmount) { balance = balance - withdrawAmount; } public void deposit(double depositAmount) { balance = balance + depositAmount; }
并从通话中删除余额参数
main
是一个静态的方法。 它不能引用balance
,这是一个属性(非静态variables)。 只有通过对象引用(例如yourAccount.balance
或yourAccount.balance
)引用时, 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; }
静态的电话撤回和存款是你的问题。 account.withdraw(余额,2500); 这条线不能工作,因为“balance”是Account的一个实例variables。 代码没有多大意义,不会将提取/存储封装在Account对象本身内部吗? 所以退出应该更喜欢
public void withdraw(double withdrawAmount) { balance -= withdrawAmount; }
当然,根据你的问题,你可以在这里做额外的validation,以防止负平衡等。
你正试图直接从静态方法访问非静态字段,这在java中是不合法的。 平衡是一个非静态的字段,所以要么使用对象引用来访问它,要么是静态的。
只要写:
private static double balance = 0;
你也可以这样写:
private static int id = 0; private static double annualInterestRate = 0; public static java.util.Date dateCreated;
你可以保持你的提取和存款方法是静态的,但是你必须像下面的代码一样写下来。 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());