使用字符串实例化时遇到麻烦

时间:2018-12-04 19:04:59

标签: instantiation

我正在尝试在我的主班级中实例化一名学生,研究生和MBA学生,并调用设置器来设置其姓氏和名字,但是我遇到了一个错误“ Student类型的setFirstName(String)方法是不适用于参数(学生)”

我的主课如下:

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);

    }
}

我的学生班:

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");



    }

我的GradStudent类:

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学生课程:

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");
    }

1 个答案:

答案 0 :(得分:0)

由于未将String参数放在双引号内(例如michael.setFirstName(“ michael”);         michael.setLastName(“ scott”);)。将函数setFirstName()和setLastName()的所有参数放在双引号中,您的问题将得到解决。祝你今天愉快!您还可以使用get函数打印每个实例的成员,以确保