我正在編寫一個程序,用於跟蹤隨着時間的推移完成的不同事務 。我有一個主班,另一個班級名爲 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()
的功能裏面我的主類。我想要做的是從我創建的函數(processCheck
和processDeposit
)中調用我創建的方法(setBalance
和setServiceCharge
)。
但我不能讓它運行,我一直運行這些錯誤消息。
非靜態方法的setBalance(雙,INT,雙,布爾值)不能從靜態上下文 CheckingAccount.setBalance引用(電車,TCODE,curCharge,monthCh);
+1在totalService'好的地方,錯過了:P – MadProgrammer
謝謝,我感謝你的幫助。這工作完美。 –