1

我正在编写一个程序来跟踪随着时间的推移完成的不同交易。我有一个主类,还有另一个名为 CheckingAccount.java.

我有一个以这种方式格式化的主类。

public class Main
{
   public static void main (String[] args)
   { 
         CheckingAccount c = new CheckingAccount(bal);
         --line of code---
         --line of code---
         --line of code---          
   }

   public static int getTransCode()
   {
         --line of code---
   }
   public static double getTransAmt()
   {
         --line of code---
   }
   public static void processCheck(double trAm, int tCode, boolean monthCh)
   {
        double curCharge=0.15;

        CheckingAccount.setBalance(trAm,tCode,curCharge,monthCh);
        CheckingAccount.setServiceCharge(curCharge);
   }
   public static void processDeposit(double trAm, int tCode, boolean monthCh)
   {
        double curCharge=0.10;

        CheckingAccount.setBalance(trAm,tCode,curCharge,monthCh);
        CheckingAccount.setServiceCharge(curCharge);
   } 
}

这是我的 CheckingAccount.java

public class CheckingAccount
{
   private double balance;
   private double totalServiceCharge;

   public CheckingAccount(double initialBalance)
   {
        balance = initialBalance;
        totalServiceCharge = totalServiceCharge;
   }

   public double getBalance()
   {
        return balance;
   }

   public void setBalance(double tAm, int Code, double charge, boolean mChrg)
   {
        if(tCode == 1)
        balance = (balance - tAm) - charge;
        else //if(tCode == 2)
            balance = (balance + tAm) - charge;
   }

   public double getServiceCharge()
   {
        return totalServiceCharge;
   }

   public void setServiceCharge(double currentServiceCharge)
   {
        totalServiceCharge = totalServiceCharge+currentServiceCharge;
   }
}

因此,我无法工作的行在我的主类的函数内部CheckingAccount.setBalance()CheckingAccount.setServiceCharge()我要做的是从我在主类(和)上创建的函数中调用我在类中创建的方法(和setBalance)。setServiceChargeprocessCheckprocessDeposit

但我无法让它运行,我继续运行这些错误消息。

不能从静态上下文中引用非静态方法 setBalance(double,int,double,boolean) CheckingAccount.setBalance(trAm,tCode,curCharge,monthCh);

4

3 回答 3

1

setBalance通过错误的类名调用您的...setBalance()方法是非静态的,因此它被定义为类的特定实例,而不是类..

**CheckingAccount.setBalance(trAm,tCode,curCharge,monthCh);
CheckingAccount.setServiceCharge(curCharge);**

您需要创建一个实例CheckingAccount来调用该方法..

其次,在你constructorCheckingAccount类中,你没有传递任何参数totalService,但是你设置了一个未知变量。你会在那里得到一个编译器错误。你需要totalServiceCharge用一个固定值初始化你,或者,你可以将它作为 main.. 的参数传递。并如下更改您的构造函数..

public CheckingAccount(double initialBalance, ** double totalServiceCharge)
{
    balance = initialBalance;
    this.totalServiceCharge = totalServiceCharge;
}

然后从 main 中,这样称呼它:-

CheckingAccount ca = new CheckingAccount(bal, totalServiceCharge);
于 2012-09-26T04:17:11.940 回答
0

一种可能的解决方案是:

您需要CheckingAccount在调用方法之前创建对象。

例子:

public static void processDeposit(double trAm, int tCode, boolean monthCh)
   {
        double curCharge=0.10;
        CheckingAccount ca = new CheckingAccount();    
        ca.setBalance(trAm,tCode,curCharge,monthCh);
        ca.setServiceCharge(curCharge);**
   } 

另一种方法是将 setBalance 方法更改为静态方法。

public static void setBalance(double tAm, int Code, double charge, boolean mChrg)
   {
        if(tCode == 1)
        balance = (balance - tAm) - charge;
        else //if(tCode == 2)
            balance = (balance + tAm) - charge;
   }

我认为第一种方法比第二种方法更有意义。

于 2012-09-26T04:10:28.137 回答
0

CheckingAccount.setBalance并且CheckingAccount.setServiceCharge不是静态方法(在这种情况下,它们不应该是)。

您需要将帐户的引用传递给您的方法...

public static void processCheck(CheckingAccount ca, double trAm, int tCode, boolean monthCh)
{
    double curCharge=0.15;

    ca.setBalance(trAm,tCode,curCharge,monthCh);
    ca.setServiceCharge(curCharge);**
}

public static void processDeposit(CheckingAccount ca, double trAm, int tCode, boolean monthCh)
{
    double curCharge=0.10;

    ca.setBalance(trAm,tCode,curCharge,monthCh);
    ca.setServiceCharge(curCharge);
} 

你将能够做类似的事情......

public static void main (String[] args)
{ 
    CheckingAccount c = new CheckingAccount(bal);
    processCheck(ca, 100.0, 1, false);
}
于 2012-09-26T04:14:42.850 回答