尝试输出 getter 时,方法为字符串输出 null,为双精度输出 0

When trying to output getter, method outputs null for strings and 0 for doubles

我是 Java 的新手,我被分配到学校做实验。本实验室要求您创建一个 class,其中包含一组带有构造函数、getter 和 setter 的实例变量。在接下来的class中,它要求你为这些变量输出变量,然而,它输出为null。

Class 有 getter 和 setter

public class ASEmployee {

        //initializes the instance variable firstname of type String
        private String firstname; 

        //initializes the instance variable last name of type string
        private String lastname;

        //initializes the instance variable monthlySalary of type double
        private double monthlySalary; 

        //makes a constructor for all three instance variables
        public ASEmployee (String userFirstname, String userLastname, 
                double userMonthlySalary) {
            setFirstname(userFirstname); 
            setLastname(userLastname); 
            setMonthlySalary(userMonthlySalary); 

        }

    //initializes a getter for firstname
    public String getFirstname() {
        return firstname; 
    }

    //initializes a setter for firstname
    public void setFirstname (String userFirstname) {
        userFirstname = firstname; 
    }

    //initializes a getter for lastname
    public String getLastname(){
        return lastname;
    }

    //initializes a setter for lastname
    public void setLastname (String userLastname) {
        userLastname = lastname; 
    }

    //initializes a  getter for monthlySalary
    public double getMonthlySalary() {
        return monthlySalary; 
    }

    //initializes a setter for monthlySalary
    public void setMonthlySalary (double userMonthlySalary) {
        if (monthlySalary >= 0.0) {
            userMonthlySalary = monthlySalary; 
        }   
    }
}

Class 我要输出的东西`

public class ASPayroll {

public static void main(String[] args) {



    //creates an object for the first employee
    ASEmployee employee1 = new ASEmployee ("Bob", "Jones", 2000.00);

    //creates an object for the second employee
    ASEmployee employee2 = new ASEmployee ("Abeba", "Tefera", 3000.00); 


    //displays the monthly salary of the first employee
    System.out.printf("%s %s %s", employee1.getFirstname(), employee1.getLastname()
            , employee1.getMonthlySalary());


    //displays the monthly salary of the second employee
    System.out.printf("%n%s %s %s", employee2.getFirstname(), employee2.getLastname(),
            employee2.getMonthlySalary());

这会将字符串输出为 "null",将双精度输出为“0.0”

你的二传手错了:

// userLastname is the parameter you are passing in.  lastname is the class 
// variable.  You are setting the parameter to the value of the uninitialized
// value of lastname.  userLastname then is thrown away the setter function ends.
userLastname = lastname; 

应该是:

// Sets your class variable to the value of the parameter being passed in.
lastname = userLastname; 

firstnamemonthlySalary 你也有这个错误。

是的,它应该 return 为 null,因为您没有正确分配它

你的 setter firstName

方法
//initializes a setter for firstname
public void setFirstname (String **userFirstname**) {
    **userFirstname** = firstname; 
}

It should be like this

//initializes a setter for firstname
public void setFirstname (String userFirstname) {
    this.firstname = userFirstname;
}

//initializes a setter for lastname
public void setLastname (String userLastname) {
    this.lastname = userLastname; 
}

//initializes a setter for monthlySalary
public void setMonthlySalary (double userMonthlySalary) {
    if (monthlySalary >= 0.0) {
        this.monthlySalary = userMonthlySalary; 
    }

}

此外,您不需要 printf 中的 %s,因为在这里我们知道 returned 的数据类型。 Why use printf("%s") for arguments passed to generic methods?

您在赋值时混淆了自己。 你应该写成: 姓氏 = 用户姓氏; 名字=用户名字

这会成功的