java 从主类上的函数调用外部类对象
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12594410/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Calling an external class object from function on main class
提问by rocker_d
I am writing a program that keeps track of different transactions
done over time. I have a main class, and also another class named
CheckingAccount.java
.
我正在编写一个程序来跟踪随着时间的推移完成的不同事务。我有一个主类,还有另一个名为
CheckingAccount.java
.
I have a main class formatted this way.
我有一个以这种方式格式化的主类。
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);
}
}
This is my CheckingAccount.java
这是我的 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;
}
}
So the lines I can't get to work are CheckingAccount.setBalance()
and CheckingAccount.setServiceCharge()
inside the functions on my main class. What I'm trying to do is to call the the methods I created (setBalance
, and setServiceCharge
) in my class, from functions that I created on my main class (processCheck
, and processDeposit
).
因此,行,我不能去工作,都CheckingAccount.setBalance()
和CheckingAccount.setServiceCharge()
我的主类的函数中。我想要做的就是打电话给我创建的方法(setBalance
和setServiceCharge
)在我的课,从功能,我在我的主类创建(processCheck
和processDeposit
)。
But I cannot get it to run, I keep running with these error messages.
但是我无法让它运行,我一直在运行这些错误消息。
non-static method setBalance(double,int,double,boolean) cannot be referenced from a static context CheckingAccount.setBalance(trAm,tCode,curCharge,monthCh);
不能从静态上下文中引用非静态方法 setBalance(double,int,double,boolean) CheckingAccount.setBalance(trAm,tCode,curCharge,monthCh);
采纳答案by Rohit Jain
You are calling your setBalance
through classname which is wrong.... setBalance()
method is non-static, so it is defined to specific instance of the class, not for a class..
您正在调用您的setBalance
through 类名,这是错误的......setBalance()
方法是非静态的,因此它被定义为该类的特定实例,而不是一个类..
**CheckingAccount.setBalance(trAm,tCode,curCharge,monthCh);
CheckingAccount.setServiceCharge(curCharge);**
You need to create an instance of CheckingAccount
to call the method..
您需要创建一个实例CheckingAccount
来调用该方法..
Secondly, in your constructor
of CheckingAccount
class, you haven't passed any argument for totalService
, but you are setting one with an unknown variable..
You will get a compiler error there..
Either you need to initialize your totalServiceCharge
with a fixed value or, you can pass it as an argument from main.. And change your constructor as below..
其次,在你constructor
的CheckingAccount
班级中,你没有为 传递任何参数totalService
,但是你设置了一个未知变量..你会在那里得到一个编译器错误..要么你需要totalServiceCharge
用固定值初始化你的将它作为参数从 main.. 并更改您的构造函数如下..
public CheckingAccount(double initialBalance, ** double totalServiceCharge)
{
balance = initialBalance;
this.totalServiceCharge = totalServiceCharge;
}
Then from main, call it like this: -
然后从主,这样称呼它: -
CheckingAccount ca = new CheckingAccount(bal, totalServiceCharge);
回答by kosa
One of the possible solution is:
一种可能的解决方案是:
You need to create object for CheckingAccount
before calling method.
您需要CheckingAccount
在调用方法之前为其创建对象。
Example:
例子:
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);**
}
Another way is, change setBalance method as static method.
另一种方法是将 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;
}
I think first approach makes more sense than second approach.
我认为第一种方法比第二种方法更有意义。
回答by MadProgrammer
CheckingAccount.setBalance
and CheckingAccount.setServiceCharge
are not static methods (and in this context, they shouldn't be).
CheckingAccount.setBalance
并且CheckingAccount.setServiceCharge
不是静态方法(在这种情况下,它们不应该是)。
You need to pass a reference of the account to your methods...
您需要将帐户的引用传递给您的方法...
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);
}
The you would be able to do something like...
你将能够做这样的事情......
public static void main (String[] args)
{
CheckingAccount c = new CheckingAccount(bal);
processCheck(ca, 100.0, 1, false);
}