-4

我创建了一个名为 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);
    }
   }
}
}
4

2 回答 2

2
private void ATM(){

那是私有的,甚至不是构造函数,而是一种方法。构造函数不应该有返回类型。

此外,ATM 不应扩展银行。

于 2012-10-31T03:37:37.517 回答
0

您的构造函数不应声明应为的返回类型:

public ATM() {
   ....
}

此外,当您有一个子类时,您必须使其通过 IS-A 测试。所有子类都应该对此测试说“是”,以便成为适当的子类。ATM IS-A 银行?嗯,不完全是。你可以为 ATM 设计一个超类,但 Bank 可能不是。一家银行可能拥有许多 ATM。ATM IS-A AccountAccessPoint 或 IS-A CashDispenser。但这更像是一种建模练习。

于 2012-10-31T03:41:02.987 回答