我正在尝试使用Salaried中的toString方法从EmployeesApplet类中的Salaried类输出信息,但是我一直收到错误
EmployeesApplet.java:292:错误:无法从静态上下文引用非静态方法toString()
ta.append(Salaried.toString());
^
如何解决此错误以正确显示信息?
这是Salaried类
{"key":"value", "key":"value", "key":"value"}
{"key":"value", "key":"value", "key":"value"}
这是EmployeesApplet类
public class Salaried extends Employee
{
private double weekly_salary;
public Salaried(String first_name, String last_name, int e, double w ) // one constructor
{
super(first_name,last_name, e);
weekly_salary = w;
}
public String toString()
{
return super.toString() + " \nWeekly Salary" + weekly_salary ;
} // toString method
}
答案 0 :(得分:1)
如错误所述,toString()不是静态的,您需要在Salaried实例上运行它。例如
Salaried s = new Salaried();
s.toString(); // should work...
如果没有EmployeeApplet的相关部分,我无法进一步建议,请注意ta.append(Salaried.toString());在您发布的错误消息中,似乎与您给出的EmployeeApplet的片段相对应...