使用字符串实例化时遇到问题

Having trouble instantiating using Strings

我正在尝试在我的主 class 中实例化一个学生、研究生和一个 MBA 学生,并调用 setter 来设置他们的名字和姓氏,但我 运行 进入错误 "The method setFirstName(String) in the type Student is not applicable for the arguments (Student)"

我的主要class如下:

public class Main {

    public static void main(String[] args) {
        Student bob = new Student();
        GradStudent john = new GradStudent();
        MBAstudent michael = new MBAstudent();

        bob.setFirstName(bob);
        bob.setLastName(smith);
        bob.setmNumber(1);
        bob.setMatriculated(true);

        john.setFirstName(john);
        john.setLastName(white);
        john.setmNumber(2);
        john.setMatriculated(true);
        john.setAge(23);

        michael.setFirstName(michael);
        michael.setLastName(scott);
        michael.setmNumber(3);
        michael.setMatriculated(true);
        michael.setGpa(4.0);

    }
}

我的学生Class:

public class Student {
    private String firstName;
    private String lastName;
    private int mNumber;
    private boolean matriculated;

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public int getmNumber() {
        return mNumber;
    }

    public void setmNumber(int mNumber) {
        this.mNumber = mNumber;
    }

    public boolean isMatriculated() {
        return matriculated;
    }

    public void setMatriculated(boolean matriculated) {
        this.matriculated = matriculated;
    }
    public String toString() {
        return (firstName + " " + lastName + " has an MNumber of " + 
                mNumber + " and is enrolled");



    }

我的研究生 Class:

public class GradStudent extends Student {
    private int age;

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
    public String toString() {
        return (getFirstName() + " " + getLastName() + " has an MNumber of " + 
                getmNumber() + " and is " + age + " years old and is enrolled");
    }

和我的 MBA 学生 Class:

public class MBAstudent extends Student {
    private double gpa;

    public double getGpa() {
        return gpa;
    }

    public void setGpa(double gpa) {
        this.gpa = gpa;
    }
    public String toString() {
        return (getFirstName() + " " + getLastName() + " has an MNumber of " + 
                getmNumber() + " and has a GPA of " + gpa + " years old and is enrolled");
    }

您可能会遇到这个问题,因为您没有将字符串参数放在双引号内(例如,michael.setFirstName("michael"); michael.setLastName("scott");)。将函数 setFirstName() 和 setLastName() 的所有参数放在双引号内,您的问题将得到解决。祝你今天过得愉快!您还可以使用get函数打印每个实例的成员以确保