나는 bank라는 서브 클래스와 ATM이라는 서브 클래스를 만들었고 서브 클래스 ATM의 인스턴스를 선언하고자합니다. 아래에 표시된대로 선언하면 오류가 발생합니다. 개인 및조차 생성자의서브 클래스의 인스턴스 선언하기
package bank;
import java.util.Scanner;
public class Bank {// Bank class
protected int PIN,chequing_balance,savings_balance;
public void set_PIN(int PIN){
this.PIN= PIN;
}
public int get_PIN(){
return this.PIN;
}
public void set_chequing_balance(int chequing_balance){
this.chequing_balance=chequing_balance;
}
public int get_chequing_balance(){
return chequing_balance;
}
public void set_savings_balance(int savings_balance){
this.savings_balance=savings_balance;
}
public int get_savings_balance(){
return savings_balance;
}
public class ATM extends Bank{// ATM is a subclass of Bank
private void ATM(){
PIN=1207605;
chequing_balance=0;
savings_balance=0;
}
public void deposit_savings(int amount){
this.savings_balance+=amount;
}
public void withdrawl_savings(int amount){
this.savings_balance-=amount;
}
public void transfer_savings_to_chequing(int amount){
this.chequing_balance+=amount;
this.savings_balance-=amount;
}
public void deposit_chequing(int amount){
this.chequing_balance+=amount;
}
public void withdrawl_chequing(int amount){
this.chequing_balance-=amount;
}
public void transfer_chequing_to_savings(int amount){
this.savings_balance+=amount;
this.chequing_balance-=amount;
}
}
public static void main(String[] args) {
int key,amount;
ATM x= new ATM(); // declaration of instance of ATM subclass
Scanner input= new Scanner(System.in);
do{
System.out.println("Please enter transaction type 1.Deposit, 2. Withdraw 3. Transfer ");
key=input.nextInt();
}while(key<1||key>3);
do{
System.out.println("Please enter desired amount");
amount=input.nextInt();
}while(amount<0);
if (key==1)
{
System.out.println("Please enter account desired 1. Chequing 2. Savings");
key=input.nextInt();
if (key==1)
{
x.deposit_chequing(amount);
}
else if(key==2)
{
x.deposit_savings(amount);
}
}
else if (key==2)
{
System.out.println("Please enter account desired 1. Chequing 2. Savings");
key=input.nextInt();
if(key==1)
{
x.deposit_chequing(amount);
}
else if(key==2)
{
x.deposit_savings(amount);
}
}
else if(key==3)
{
System.out.println("Please enter type of transaction you want 1. Chequing to Savings 2. Savings to Chequing");
key=input.nextInt();
if (key==1)
{
x.transfer_chequing_to_savings(amount);
}
else if (key==2)
{
x.transfer_savings_to_chequing(amount);
}
}
}
}
그리고 오류는 ...? –
새 ATM을 만들 때 내 생성자를 만든 후 여전히 내 기본 메서드에서 '정적 변수가 아닌 정적 변수입니다. 정적 컨텍스트에서 참조 할 수 없습니다.' – noname