从主类的函数调用外部类对象

时间:2012-09-26 04:08:28

标签: java class function main

我正在编写一个跟踪不同交易的程序 随着时间的推移。我有一个主类,还有另一个名为的类 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上创建的函数调用我在我的类中创建的方法(setServiceChargeprocessCheck),以及processDeposit)。

但我无法让它运行,我一直在运行这些错误消息。

  

非静态方法setBalance(double,int,double,boolean)无法从静态上下文引用           CheckingAccount.setBalance(电车,TCODE,curCharge,monthCh);

3 个答案:

答案 0 :(得分:1)

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

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

您需要创建CheckingAccount的实例来调用方法..

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

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

然后从main,这样称呼: -

CheckingAccount ca = new CheckingAccount(bal, totalServiceCharge);

答案 1 :(得分: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;
   }

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

答案 2 :(得分:0)

CheckingAccount.setBalanceCheckingAccount.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);
}