-2

スーパークラスの変数にリンクする場合: FirstName LastName StreetAddress PostCode PhoneNumber

コードは正常に動作しますが、サブクラス DepartmentName TFN AnnualSalary の 3 つの変数を追加すると、

変数は見つかりませんが、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" );


            }
        }

上記の 2 つのコード サンプルをそのまま使用すると、シンボルが見つからないというエラーが発生します。(ここでは TFN を使用していますが、3 つの変数のいずれかでエラーが発生します。)

 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

You have not added the getters for the new fields TFN & departmentName. As the two variables are declared private so can't access them directly either. You are trying to call the getter

curShopEmployee.getTFN()

しかし、そのメソッドはあなたのクラスには存在しません。

于 2013-06-09T07:37:07.737 に答える