链接到超类的变量时: 名字 姓 街道地址 邮编 ******中国
代码工作正常,但如果我添加一个子类的三个额外变量 部门名称 TFN AnnualSalary
找不到变量,bluej但是,VISUALLY连接到子类。
我只是摆脱了额外的变量,但是如果我做的话我的代码没有编译,那么现在,变量就在那里,但没有被完全使用。
代码:
数组列表
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
}
}
谢谢。任何帮助表示赞赏。
答案 0 :(得分:0)
您尚未为新字段TFN&amp;添加getter。部门名称。由于这两个变量被声明为私有,因此也无法直接访问它们。你试图打电话给吸气剂
curShopEmployee.getTFN()
但您的班级中不存在该方法。