繁体   English   中英

施工人员故障排除

[英]Constructor trouble shooting

这是我的项目:

package myProjects;

import java.awt.GridBagConstraints;

import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.*;

public class GeometryEquationSolver extends JFrame{

JPanel mainPanel;
JTextField eq1Text, eq2Text;
JButton enterButton, clearTextButton;

public static void main(String[] args) {
    new GeometryEquationSolver();
}
public GeometryEquationSolver(){
    this.setSize(340, 140);
    this.setTitle("Equation Solver");
    this.setDefaultCloseOperation(EXIT_ON_CLOSE);
    this.setLocationRelativeTo(null);

    mainPanel = new JPanel();
    mainPanel.setLayout(new GridBagLayout());

    addItem(new JLabel("Equation 1"), 0, 0, 1, 1);
    addItem(new JLabel("Equation 2"), 2, 0, 1, 1);

    eq1Text = new JTextField(10);
    addItem(eq1Text, 0, 1, 1, 1);
    eq2Text = new JTextField(10);
    addItem(eq2Text, 2, 1, 1, 1);

    addItem(new JLabel("="), 1, 1, 1, 1); // Just the "=" for looks.

    enterButton = new JButton("Enter");
    enterButton.addActionListener(e->{
        Equation eq1 = new Equation(eq1Text.getText());
    });
    addItem(enterButton, 0, 2, 1, 1);

    clearTextButton = new JButton("Clear");
    clearTextButton.addActionListener(e->{
        eq1Text.setText(null);
        eq2Text.setText(null);
    });
    addItem(clearTextButton, 2, 2, 1, 1);

    this.add(mainPanel);
    this.setVisible(true);
}
private String[] rawData;
private String[] splitEq(String eq){
    rawData = eq.split("");
    return rawData;
}
private void addItem(JComponent c, int x, int y, int width, int height){
    GridBagConstraints gbc = new GridBagConstraints();
    gbc.gridx = x;
    gbc.gridy = y;
    gbc.gridwidth = width;
    gbc.gridheight = height;
    gbc.weightx = 100.0;
    gbc.weighty = 100.0;
    gbc.fill = GridBagConstraints.NONE;

    mainPanel.add(c, gbc);
}
}
class Equation{

public enum opperation {add, sub, mult, div};
public opperation opper;
public String eq;

public Equation(String eq){
    this.eq = eq;
    opper = opperation.add;
    this.Equation(this.eq, opper);
}
 public Equation(String eq, opperation opper){

    }
}

我的问题出现在类方程式中。 在第一个构造函数中,我必须执行以下操作:

this.Equation(this.eq, opper);

我得到这个错误:

未为类型方程式定义方法方程式(String,Equation.opperation)

我试图在第一个之后调用构造函数。 有谁知道如何解决这一问题?

正确的语法是this(this.eq, opper); 但是,由于此行必须是构造函数代码中的第一行,因此它将变为this(eq, opperation.add);

因此,您的构造函数最终将如下所示:

public Equation(String eq){
    this(eq, opperation.add);
    this.eq = eq;
    opper = opperation.add;
    //this.Equation(this.eq, opper);
}

如果绝对有必要在调用其他构造函数之前执行一些代码,则此答案可能会有所帮助。

如果要从构造函数调用构造函数,请参考https://docs.oracle.com/javase/tutorial/java/javaOO/thiskey.html

这是可以工作的代码。

public class Equation {

    public static enum opperation {add, sub, mult, div};
    public opperation opper;
    public String eq;

    public Equation(String eq){
        this(eq, opperation.add);
        this.eq = eq;
        opper = opperation.add;
    }
    public Equation(String eq, opperation opper){

    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM