Java 从主类上的函数调用外部类对象
我正在写一个程序来跟踪不同的交易 随着时间的推移。我有一个主类,还有一个名为Java 从主类上的函数调用外部类对象,java,class,function,main,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---
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
,和setServiceCharge
),这些方法来自我在主类(processCheck
,和processDeposit
)上创建的函数
但我无法让它运行,我一直在运行这些错误消息
不能从静态上下文引用非静态方法setBalance(double、int、double、boolean)
检查账户余额(有轨电车、tCode、curCharge、monthCh)强>
可能的解决办法之一是: 在调用方法之前,需要为
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);**
}
另一种方法是,将立根平衡法改为静态法
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;
}
我认为第一种方法比第二种方法更有意义。可能的解决方案之一是: 在调用方法之前,需要为
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);**
}
另一种方法是,将立根平衡法改为静态法
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;
}
我认为第一种方法比第二种方法更有意义。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);
}
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);
}
您正在通过类名调用您的
setBalance
,这是错误的setBalance()
方法是非静态的,所以它是针对类的特定实例定义的,而不是针对类
**CheckingAccount.setBalance(trAm,tCode,curCharge,monthCh);
CheckingAccount.setServiceCharge(curCharge);**
您需要创建一个CheckingAccount
的实例来调用该方法
其次,在CheckingAccount
类的构造函数中,您没有为totalService
传递任何参数,但是您正在使用未知变量设置一个参数。。
您将在那里得到一个编译器错误。。
您需要使用固定值初始化您的totalServiceCharge
,或者,您可以将其作为main的参数传递。。并更改您的构造函数,如下所示
public CheckingAccount(double initialBalance, ** double totalServiceCharge)
{
balance = initialBalance;
this.totalServiceCharge = totalServiceCharge;
}
然后从main开始,这样称呼它:-
CheckingAccount ca = new CheckingAccount(bal, totalServiceCharge);
您正在通过类名调用您的setBalance
,这是错误的setBalance()
方法是非静态的,所以它是针对类的特定实例定义的,而不是针对类
**CheckingAccount.setBalance(trAm,tCode,curCharge,monthCh);
CheckingAccount.setServiceCharge(curCharge);**
您需要创建一个CheckingAccount
的实例来调用该方法
其次,在CheckingAccount
类的构造函数中,您没有为totalService
传递任何参数,但是您正在使用未知变量设置一个参数。。
您将在那里得到一个编译器错误。。
您需要使用固定值初始化您的totalServiceCharge
,或者,您可以将其作为main的参数传递。。并更改您的构造函数,如下所示
public CheckingAccount(double initialBalance, ** double totalServiceCharge)
{
balance = initialBalance;
this.totalServiceCharge = totalServiceCharge;
}
然后从main开始,这样称呼它:-
CheckingAccount ca = new CheckingAccount(bal, totalServiceCharge);