I';已经定义了构造函数haven';不是吗?为什么赢了';不是吗?!JAVA
我正在为计算器编写一个程序,我已经将它添加到我的主Java文件中`I';已经定义了构造函数haven';不是吗?为什么赢了';不是吗?!JAVA,java,class,constructor,listener,implements,Java,Class,Constructor,Listener,Implements,我正在为计算器编写一个程序,我已经将它添加到我的主Java文件中` CalculatorEngine calcEngine = new CalculatorEngine(); 这将链接到我的类文件,因此: public class CalculatorEngine implements ActionListener {//code here ;} 谁能告诉我我做错了什么 这是错误消息:“构造函数CalculatorEngine()未定义”,但我想这就是您定义它的方式 import
CalculatorEngine calcEngine = new CalculatorEngine();
这将链接到我的类文件,因此:
public class CalculatorEngine implements ActionListener {//code here ;}
谁能告诉我我做错了什么
这是错误消息:“构造函数CalculatorEngine()未定义”,但我想这就是您定义它的方式
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JButton;
public class CalculatorEngine implements ActionListener {
Calculator parent; //a reference to Calculator window
char selectedAction = ' '; // +, -, /, or *
double currentResult =0;
// Constructor stores the reference to the Calculator
// window in the member variable parent
CalculatorEngine(Calculator parent){
this.parent = parent;
}
public void actionPerformed(ActionEvent e){
// Get the source of this action
JButton clickedButton = (JButton) e.getSource();
String dispFieldText=parent.displayField.getText();
double displayValue=0;
//Get the number from the text field // if it’s not empty
if (!"".equals(dispFieldText)){
displayValue=Double.parseDouble(dispFieldText);
}
Object src = e.getSource();
//For each action button memorise selected
//action +, -, /, or *, store the current value
//in the currentResult, and clean up the display
//field for entering the next number
if (src == parent.buttonPlus){ selectedAction = '+';
currentResult=displayValue; parent.displayField.setText("" );
} else if (src == parent.buttonMinus){ selectedAction = '-';
currentResult=displayValue; parent.displayField.setText("");
}else if (src == parent.buttonDivide){ selectedAction = '/';
currentResult=displayValue; parent.displayField.setText("");
} else if (src == parent.buttonMultiply){ selectedAction = '*';
currentResult=displayValue; parent.displayField.setText("" );
} else if (src == parent.buttonEqual){
//Perform the calculations based on selectedAction
//update the value of the variable currentResult
//and display the result
if (selectedAction=='+'){
currentResult +=displayValue;
//Convert the result to String by concatenating
//to an empty string and display it
parent.displayField.setText(""+currentResult );
}else if (selectedAction=='-'){ currentResult -=displayValue;
parent.displayField.setText(""+currentResult); }
else if (selectedAction=='/'){
currentResult /=displayValue;
parent.displayField.setText(""+currentResult); }
else if (selectedAction=='*'){
currentResult*=displayValue;
parent.displayField.setText(""+currentResult);
}
} else{
// For all numeric buttons append the button's
// label to the text field
String clickedButtonLabel= clickedButton.getText();
parent.displayField.setText(dispFieldText + clickedButtonLabel);
}}}
这是类。您的代码包含一个具有包级别(默认)权限的构造函数
CalculatorEngine(Calculator parent){
this.parent = parent;
}
因此,您不能获得默认构造函数,也不能调用除了同一个包(或子类)以外的任何地方都使用计算器的构造函数。添加一个空的公共构造函数(或者删除现有的构造函数,您将获得默认构造函数)
您正在尝试不向CalculatorEngine
的唯一构造函数传递任何内容。但是,该构造函数接受一个参数,即计算器
将计算器对象传递给构造函数
public CalculatorEngine(){
super();
}
CalculatorEngine calcEngine = new CalculatorEngine(aCalculatorObject);
这是构造器:
CalculatorEngine(Calculator parent)
因此,在构造时必须提供计算器。您不能调用new CalculatorEngine()
,因为没有无参数构造函数
public CalculatorEngine(){
super();
}
CalculatorEngine calcEngine = new CalculatorEngine(aCalculatorObject);
请注意,在没有任何其他构造函数的情况下,无参数构造函数可供您使用,而无需定义。请参见考虑提供一份说明您的问题的报告。这将减少混乱和更好的响应请在帖子中显示CalculatorEngine
类中的所有构造函数。@James单击并将代码添加到帖子中。粘贴代码后,将其高亮显示,然后按ctrl-kSorry键,添加上面的类代码。为响应干杯好吧,您的构造函数是:CalculatorEngine(Calculator)
,但您正在调用new CalculatorEngine()
。。。那么,到底是哪里出了问题?对不起,我不明白。我需要换公共课吗?在本例中,我尝试了这个方法,但它不允许我实现操作侦听器。您可以添加一个公共无参数构造函数,删除当前构造函数,或者调用它(假设您在同一个包中),或者修改当前构造函数并将其公开(但你还必须通过一个计算器)。我只发布了构造函数。我想我已经解释得够多了,但Elliot Frisch的回答解释得更好。是的,但默认构造函数不再可用是有原因的;)为你所有的回答干杯。我想我不明白你的帮助是因为我需要自己查资料。再次感谢!