2012-12-20 4 views
-1

두 클래스, 하나는 Driver, 다른 하나는 BankAccount입니다. Driver에는 Driver라는 메소드가 있고 BankAccount에는 Deposit이라는 메소드가 있습니다. 내 드라이버 메서드에서 BankAccount.Deposit를 호출하려고 할 때 "정적이 아닌 메서드 Deposit()을 정적 컨텍스트에서 참조 할 수 없습니다"라는 오류가 나타납니다.다른 클래스에서 다른 클래스 및 메서드로 메서드 호출

이 코드를 실행하기 위해 내가해야 할 일에 대한 조언. 여기

import javax.swing.JOptionPane; 
public class Driver 
{ 
    int choice; 
    String number; 
    //public Driver() 
    public Driver() 
    { 
     String number = JOptionPane.showInputDialog("1. Deposit 2. Withdraw 3. Balance 4. Change name 5. Exit"); 
     int choice = Integer.parseInt(number); 
     do 
     { 
     if(choice == 1) 
     { 
      BankAccount.Deposit() = new Deposit(); 
      Driver.Driver = new Driver(); 
      }else if(choice == 2) 
      { 
       BankAccount.Withdrawl = new Withdrawl(); 
       Driver.Driver = new Driver(); 
      }else if(choice == 3) 
      { 
       BankAccount.getBalance = new getBalance(); 
       JOptionPane.showDialog(balance); 
       Driver.Driver = new Driver(); 
      }else if(choice == 4) 
      { 
       name = JOptionPane.showInputDialog(" Please enter a name"); 
       Driver.Driver = new Driver(); 
      }else if(choice ==5) 
      { 
       JOptionPane.showDialog("Goodbye" + name); 
      } 
     }while(choice >= 1 && choice <= 5); 
} 
} 

는이 같은 코드를 작성한 이유를 이해하지 않습니다는 BankAccount 방법

import javax.swing.JOptionPane; 
public class BankAccount 
{ 
double balance = 400; 
double deposit; 
double withdraw; 
double Interest = 1.05; 
String name; 
String accountNumber; 

public BankAccount() 
{ 
name = null; 
accountNumber = null; 
balance = 0; 
} 

public double Deposit() 
{ 
    String input = JOptionPane.showInputDialog("How much would you like to deposit?"); 
    deposit = Integer.parseInt(input); 
    if (deposit < 10000) 
    { 
     balance = (deposit + balance); 

    } 
    return balance; 
} 

} 
+1

'새로운 예금()'? 'Deposit'은 _class_입니까? 어디서 어떻게 정의되어 있습니까? –

+2

이 사이트에서 이미 여러 번 언급했듯이 - 입문용 자바 텍스트를 가져 오거나 다시 읽는 것을 고려해야합니다. 코드의 오류 수가 중요합니다. – Perception

+1

비슷한 질문을 먼저 검색하십시오. 다음을보십시오 : http://stackoverflow.com/search?q=non-static+method+Deposit%28%29+cannot+be+referenced+from+a+static+context+[java] – RAS

답변

1

입니다.

java의 메소드 이름은 이 아닌 Deposit과 같은 작은 문자로 시작해야합니다.

BankAccount은 클래스이고 Deposit은 비 정적 메서드입니다.

b.Deposit(); 
b.Withdraw(); 

당신은해야한다 : 그 객체 참조를 사용하는 방법을 사용하여 다음

BankAccount b =new BankAccount(); 

을 그리고 :

그래서 Deposit 방법을 사용하기 위해 먼저이처럼 BankAccount 클래스의 객체/인스턴스를 만들어야합니다 다음과 같이 작성하십시오 :

if(choice == 1) 
{ 
    BankAccount b = new BankAccount(); 
    b.Deposit(); 
} 

같은

BankAccount.Deposit() = new Deposit(); 

가 이해되지 않는다 : 철수 및 기타

else if(choice == 2) 
{ 
    BankAccount b = new BankAccount(); 
    b.Withdrawl(); 
    Driver.Driver = new Driver(); 
} 
0

이 문을 위해 당신이 할 필요가있다. 먼저 Deposit() 방법 BankAccount입니다. BankAccount의 특정 인스턴스에 대해서만 호출하는 것이 좋습니다. 그것이 컴파일러가 불평하는 것입니다.

그 외에도 Deposit()int 값을 반환하며 이는 할당 문의 왼쪽에 나타날 수있는 문제가 아닙니다. 또한 Deposit이라는 클래스가 없으므로 new Deposit()이 무엇인지 알 수 없습니다.

코드에서 유사한 문제가있는 것으로 보입니다. 예를 들어, 다음 문 :

Driver.Driver = new Driver(); 

는 더 필드 Driver.Driver이없는 완전한 넌센스 —입니다.

자습서 Understanding Instance and Class Members을 읽는 것이 좋습니다.

0

는 '의도'의 작업 버전 :

import javax.swing.JOptionPane; 
public class Driver 
{ 
    int choice; 
    String number; 
    BankAccount myAccount=null; 

    public Driver() 
    { 
    myAccount=new BankAccount(); 
    } 

    public void drive() 
    { 
    String number = ""; 
     int choice = 0; 
     String name = "?"; 
     do 
     { 
      number = JOptionPane.showInputDialog("1. Deposit 2. Withdraw 3. Balance 4. Change name 5. Exit"); 
      choice = Integer.parseInt(number); 
     if(choice == 1) 
     { 
      myAccount.Deposit(); 
      }else if(choice == 2) 
      { 
      // mAccount.Withdrawl(); 
      // missing method Withdraw1() 
      }else if(choice == 3) 
      { 
     // BankAccount.getBalance = new getBalance(); 
     // missing method getBalance() 
     // JOptionPane.showDialog(balance); 
      }else if(choice == 4) 
      { 
       JOptionPane.showInputDialog(" Please enter a name"); 
     // todo i guess name should be used somewhere in bankaccount... like myAccount.setName(name) 
      }else if(choice ==5) 
      { 
       // JOptionPane.showDialog("Goodbye" + name); 
     // no showDialog method in JOptionPane 
     return; 
      } 
     }while(choice >= 1 && choice <= 5); 
    } 

    public static void main(String pArgs[]) 
    { 
    Driver driver=new Driver(); 
    driver.drive(); 
    } 
}