2011-11-12 13 views
6

Przepraszam z wyprzedzeniem, jeśli ten kod nie jest poprawnie sformatowany, próbując wkleić zamiast przepisywania każdej linii. Jeśli to nie jest w porządku, czy ktoś może mi w prosty sposób wkleić wiele linii kodu jednocześnie?nie można utworzyć statycznego odwołania do niestatycznego pola

Moim głównym pytaniem jest to, że wciąż otrzymuję komunikat o błędzie informujący: Cannot make a static reference to the non-static field balance.

Próbowałem czyniąc metody statyczne, bez skutku, a co główne metody non-statycznego poprzez usunięcie „statyczne” z nagłówka , ale potem pojawia się komunikat: java.lang.NoSuchMethodError: main Exception in thread "main"

Czy ktoś ma jakieś pomysły? Każda pomoc jest doceniana.

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; 
    } 
} 
+0

ja "nie jestem pewien, dlaczego nawet równowagę jako argument do account.withdraw() i account.deposit() metody. Od salda rachunku _knows_, najprostszym rozwiązaniem jest usunięcie ich z metodami Alternatywnie, jeśli naprawdę chcesz je, musisz powiedzieć account.balance w wywołaniach z main(). – user949300

+1

jak formatowanie zmian kart do spacji i wybierz kod po wklejeniu i naciśnij ctrl-k, aby automatycznie wcięcić –

Odpowiedz

6

linie

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

może chcesz, aby wycofać i depozyt non-statyczne i niech zmodyfikować balans

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

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

i usunąć parametr balansu od wezwania

1

Statyczne połączenia do wypłaty i depozytu są Twoim problemem. account.withdraw (saldo, 2500); Ten wiersz nie działa, ponieważ "saldo" jest zmienną instancji konta. Kod i tak nie ma większego sensu, czy wycofanie/deponowanie nie byłoby zamknięte wewnątrz samego obiektu Konta? więc wycofać powinno być więcej jak

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

Oczywiście w zależności od problemu można zrobić dodatkowej weryfikacji, żeby uniknąć ujemnego salda itp

10

main jest metoda statyczna. Nie może odnosić się do balance, która jest atrybutem (zmienną niestatyczną). balance ma znaczenie tylko wtedy, gdy jest przekazywane przez odniesienie do obiektu (takie jak myAccount.balance lub yourAccount.balance). Ale nie ma to żadnego znaczenia, gdy jest przekazywane przez klasę (np. Account.balance (którego saldo to jest?))

Wprowadziłem kilka zmian w twoim kodzie, aby je skompilować.

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

oraz:

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

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

Wystarczy napisać:

private static double balance = 0; 

i można również napisać ci tak:

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

próbujesz uzyskać dostęp non pola statycznego bezpośrednio z metody statycznej, która nie jest le gal w java. Balans to pole niestatyczne, więc albo uzyskaj dostęp do niego za pomocą odwołania do obiektu, albo uczyń go statycznym.

-1

możesz zachować metody wypłat i depozytu statyczne, jeśli chcesz, ale musisz napisać to tak jak poniższy kod. sb = saldo początkowe i eB = saldo końcowe.

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()); 
Powiązane problemy