跟着某实例走,修改了类名然后empOne.setSalary(1000.0);处报错,不知道原因出在哪。
public class Test001 {public String name;
private double salary;
public Test001 (String empName) {
name = empName;
}
public Test001 (double empSal) {
salary = empSal;
}
public void printEmp() {
System.out.println("名字 :"+name);
System.out.println("薪水 :"+salary);
}
public static void main(String[] args) {
Test001 empOne = new Test001("RUNOOB");
empOne.setSalary(1000.0);//此处报错
empOne.printEmp();
}
} 已解决,salary的值没有正确赋予,,, 本帖最后由 isdkz 于 2023-3-9 17:13 编辑
你没有写 setSalary 方法
public class Test001 {
public String name;
private double salary;
public Test001 (String empName) {
name = empName;
}
public void setSalary (double empSal) { // 这个应该是 setSalary
salary = empSal;
}
public void printEmp() {
System.out.println("名字 :"+name);
System.out.println("薪水 :"+salary);
}
public static void main(String[] args) {
Test001 empOne = new Test001("RUNOOB");
empOne.setSalary(1000.0);//此处报错
empOne.printEmp();
}
}
页:
[1]