无法理解类行为{JAVA接口}

时间:2018-04-18 10:57:35

标签: java interface

interface BankTransaction
{ 
    void transcation(); 
}
class Customer 
{
    protected double rate;

    //do something
}

class CurrentAccount extends Customer implements BankTransaction
{
    private double x;

    // do something

    public void transcation()
    {   
        // x is not able to communicate with rate
        x = rate * 1 / 100;
    }       
} 

当我这样做时程序工作

interface BankTransaction
{ 
    void transcation(Customer C); 
}
class Customer 
{
    protected double rate;

    //do something
}

class CurrentAccount extends Customer implements BankTransaction
{
    private double x;

    // do something

    public void transcation(Customer C)
    {
        x = C.rate * 1 / 100;
    }       
} 

我知道如果我通过Customer功能致电transcation,它就能正常工作。

问题是,为什么CurrentAccount班级不能与Customer班级进行沟通,即使我已经扩展了它?

为什么第一个程序不起作用?

设率= 10,我想在费率的帮助下计算x,答案应该是0.1,但每次都是0。

Java文件:

interface BankTransaction
{ 
  void transcation(); 
 }
 class Customer
 { 
   Scanner KB = new Scanner(System.in);
   private int code;
   private String name;
   protected double balance;
    void openAccount()
    {
      System.out.print("Enter Code:");
      code = KB.nextInt();
      System.out.print("Enter Name:");
      Scanner KB = new Scanner(System.in);
      name = KB.nextLine();
      System.out.print("Enter balance:");
      balance = KB.nextDouble();
    }
    public void showAccount()
    {
      System.out.println("Code is "+code);
      System.out.println("Name is "+name);
      System.out.println("Balance is "+balance);
    }
 }
class CurrentAccount extends Customer implements BankTransaction
{ 
  private double totaltransaction;
  private double totaltax;
  public void transcation()
  { totaltransaction++;
    totaltax=balance*1/100;
  }
  public void showAccount()
  {
    System.out.println("Total Tax is "+totaltax);
    System.out.println("Total Transaction is "+totaltransaction);
  }
}


class Test
{ 
  public static void main(String arg[])
   { 
     Customer  C=new Customer();
     C.openAccount();
     C.showAccount();
     BankTransaction B;
     CurrentAccount CA=new CurrentAccount();
     B=CA;
     B.transcation();
     CA.showAccount();


   }
}

2 个答案:

答案 0 :(得分:0)

代码的问题在于主要功能。

class Test { 
    public static void main(String arg[]) {
        Customer  C=new Customer();
        C.openAccount();
        C.showAccount();
        BankTransaction B;
        CurrentAccount CA=new CurrentAccount();
        B=CA;
        B.transcation();
        CA.showAccount();
   }
}

您正在创建的Customer C对象未链接到您正在创建的CurrentAccount CA对象。由于CA继承了Customer对象,因此它还继承了公共openAccount()方法。所以你可以直接在CA对象上调用它。 transcation()方法也是如此。

只需将您的主要功能更改为此功能,即可使用:

class Test { 
    public static void main(String arg[]) {
        CurrentAccount CA=new CurrentAccount();
        CA.openAccount();
        CA.transcation();
        CA.showAccount();
    }
}

输出:

Enter Code:123
Enter Name:ABC
Enter balance:10
Total Tax is 0.1
Total Transaction is 1.0

答案 1 :(得分:0)

您的main方法是您的错误所在:

Customer  C=new Customer();
C.openAccount();
C.showAccount();
BankTransaction B;
CurrentAccount CA=new CurrentAccount();
B=CA;
B.transcation();
CA.showAccount();

在这里,您创建了一个Customer对象和一个CurrentAccount对象。虽然CurrentAccount继承自Customer,但这两个对象完全独立。对一个对象的更改不会影响另一个对象。

这意味着当您致电CA时,code的字段(namebalanceopenAccount)将无法设置C。相反,将设置C个字段。

当您执行B=CA时,BCA现在引用相同的CurrentAccount对象。因此,B.transaction将使用totalTax对象的字段更改CurrentAccount对象的CurrentAccount,这些字段未设置!

要解决此问题,请不要创建额外的Customer对象,只使用一个CurrentAccount对象:

CurrentAccount  C=new CurrentAccount();
C.openAccount();
C.showAccount();
BankTransaction B;
B=C;
B.transcation();
C.showAccount();