-2

链接到超类的变量时:FirstName LastName StreetAddress PostCode PhoneNumber

代码工作正常,但如果我添加子类 DepartmentName TFN AnnualSalary 的三个额外变量

找不到变量,bluej 但是,视觉上连接到子类。

我只想摆脱额外的变量,但如果我这样做,我的代码将无法编译,所以现在,变量在那里但没有被完全使用。

代码:

数组列表

private ArrayList<Person> myShopEmployee = new ArrayList<Person>();
    private JFrame frame;

    private JLabel labFirstName;   
    private JTextField txtFirstName;

    private JLabel labLastName;   
    private JTextField txtLastName;

    private JLabel labStreetAddress;   
    private JTextField txtStreetAddress;

    private JLabel labPostCode;   
    private JTextField txtPostCode;

    private JLabel labPhoneNumb;   
    private JTextField txtPhoneNumb;

    private JLabel labDepartmentName;   
    private JTextField txtDepartmentName;

    private JLabel labTFN;   
    private JTextField txtTFN;

    private JLabel labAnnualSalary;   
    private JTextField txtAnnualSalary;

    private int index = 0;

如果我从这行代码中删除额外的变量,并且上面的数组我的代码不会编译。

class creatingShopEmployeeListener implements ActionListener 
        {
            public void actionPerformed(ActionEvent event)
            {
                JOptionPane.showMessageDialog(frame,
                                            "example", JOptionPane.ERROR_MESSAGE);

                                          ShopEmployee newShopEmployee = new ShopEmployee (txtFirstName.getText(),txtLastName.getText(),txtStreetAddress.getText(),txtPostCode.getText(),txtPhoneNumb.getText(),txtDepartmentName.getText(),txtTFN.getText(),txtAnnualSalary.getText());
                                          myShopEmployee.add(newShopEmployee);
                JOptionPane.showMessageDialog(frame,
                                            "ShopEmployee has been created" );


            }
        }

如果上述两个代码示例保持原样,这就是发生找不到符号错误的地方。(这里我有 TFN,但是三个变量中的任何一个都会发生错误。)

 class seeShopEmployeeListener implements ActionListener 
        {
            public void actionPerformed(ActionEvent event)
            {
                     if (myShopEmployee.size() > 0) 
                     {

                         ShopEmployee curShopEmployee = (ShopEmployee) myShopEmployee.get(index);
                         txtFirstName.setText(curShopEmployee.getFirstName());
                         txtLastName.setText(curShopEmployee.getLastName() );
                         txtStreetAddress.setText(curShopEmployee.getStreetAddress() );
                         txtPostCode.setText(curShopEmployee.getPostCode() );
                         txtPhoneNumb.setText(curShopEmployee.getPhoneNumb() );
                         txtTFN.setText(curShopEmployee.getTFN() );
                     if ( index < myShopEmployee.size() -1 ) { index ++; }
                     else {
                            JOptionPane.showMessageDialog(frame,
                                "No ShopEmployee" );
                            } 
                }
            }
        }

最后这是 ShopEmployee 类

public class ShopEmployee extends Person
{
    //creating variables
    private String departmentName;
    private String TFN, annualSalary;
    public ShopEmployee()//ShopEmployee constructor
    {

        //setting values for the new variables
        departmentName = "";
        TFN = "";
        annualSalary = "";

    }
    public ShopEmployee(String myFirstName, String myLastName, String myStreetAddress, String myPostCode, String myPhoneNumb, String myDepartmentName, String myTFN, String myAnnualSalary)//ShopEmployee constructor which contains these variables
    {

        super(myFirstName,myLastName,myStreetAddress,myPostCode,myPhoneNumb);
        departmentName = myDepartmentName;
        TFN = myTFN;
        annualSalary = myAnnualSalary;

    }
    //setting up the variables
     public void setDepartmentName(String myDepartmentName)
    {
        setDepartmentName(myDepartmentName);
    }
    public void setTFN(String myTFN)
    {
        setTFN(myTFN);
    }
    public void setAnnualSalary(String myAnnualSalary)
    {
        setAnnualSalary(myAnnualSalary);
    }
    public String toString()
    {
        return super.toString() + ", TFN is " + TFN + ", Annual Salary is " + annualSalary + ", and Department Name is " + departmentName;//returns the super variable string which includes the name phone number ect then adds on the new variables
    }
}

谢谢你。任何帮助表示赞赏。

4

1 回答 1

0

您尚未为新字段 TFN 和部门名称添加 getter。由于这两个变量被声明为私有,因此也不能直接访问它们。您正在尝试调用 getter

curShopEmployee.getTFN()

但是您的班级中不存在该方法。

于 2013-06-09T07:37:07.737 回答