java 无法通过showStudentInformation()方法打印“s1”对象[已关闭]

xytpbqjk  于 2023-04-10  发布在  Java
关注(0)|答案(2)|浏览(141)

**已关闭。**此问题为not reproducible or was caused by typos。当前不接受答案。

这个问题是由一个错字或一个无法再复制的问题引起的。虽然类似的问题可能是on-topic在这里,但这个问题的解决方式不太可能帮助未来的读者。
9小时前关闭
Improve this question
接下来是我认为是我的练习java文件的成品。我运行了TestStudent.java文件,并收到了除了s1对象之外的所有正确输出。我已经尝试了“this”操作符和“get”方法,用于showStudentInformation()方法中的name,studentNumber和course,但仍然没有成功。

/*
    Given the partial code for the class Student that contains 3 attributes: name, studentNumber and course.
    You are to provide the setter and getter methods as well as the body of the constructor that has a missing body.
    You are also required to provide an implementation to the method showStudentInformation() so that once it is 
    called it should display the name, studentNumber and course of the Student object.
*/   

public class Student
{
    private String name;
    private int studentNumber;
    private String course;

    // this is constructor #1 - no need to add anything here
    public Student()
    {}

    // this is constructor #2 - you have to provide the body of this constructor
    public Student(String name, int studentNumber, String course)
    {
        name = this.name;
        studentNumber = this.studentNumber;
        course = this.course;
    }

    // put your setters here
    public void setName(String n) {
        this.name = n;
    }

    public void setStudentNum(Integer s) {
        this.studentNumber = s;
    }

    public void setCourse(String c) {
        this.course = c;
    }

    // put your getters here
    public String getName() {
        return this.name;
    }

    public String getCourse() {
        return this.course;
    }

    public Integer getStudentNo() {
        return this.studentNumber;
    }

    public String showStudentInformation()
    {
        return name + " " + studentNumber + " " + course; 
        // this method should display the name, studentNumber and the course of the student object.
    }
}
public class TestStudent
{
    public static void main(String args[])
    {
        Student s1 = new Student("James", 1111, "Computer Science");
        Student s2 = new Student();
        Student s3 = new Student();

        // set the properties of Student s2, assign a name, studentNumber and course
                s2.setName("Koda");
                s2.setStudentNum(2222);
                s2.setCourse("Business");
        // set the properties of Student s3, assign a name, studentNumber and course
                s3.setName("Bob");
                s3.setStudentNum(3333);
                s3.setCourse("IT");

        // print the identity of Student s1
                s1.showStudentInformation();

        // print the identity of Student s2 by calling the getter methods
                System.out.println(s2.getName() + s2.getStudentNo() + s2.getCourse());
        // print the identity of Student s3 by calling the showStudentInformation() method
                s3.showStudentInformation();
    }   

}

我使用的是在线编译器,目录中的文件是/home/Student.java和/home/TestStudent.java
注意有2个java文件

mcdcgff0

mcdcgff01#

在你的构造函数中,有些东西错了。
必须是“this.x = x”,而不是“x = this.x”。

cygmwpex

cygmwpex2#

兄弟。你的构造器打错了。应该是:

public class Student
{
    private String name;
    private int studentNumber;
    private String course;

    public Student(String name, int studentNumber, String course)
    {
        this.name = name;
        this.studentNumber = studentNumber;
        this.course = course;
    }

}

this将引用类中的属性,如private String name等。因此变量赋值没有正确完成。

//refers to the               //by writing `this`
// constructor parameter      // it will refer to the
// `studentNumber`            // class attribute `private int studentNumber`
studentNumber       =         this.studentNumber;

你已经交换了变量赋值,这就是为什么你看到null的原因,因为你的类属性没有值。你没有使用构造函数参数来设置类属性的值,而是使用类属性来设置构造函数参数的值。阅读This
另外,我不建议你使用public String showStudentInformation(),java中有一个叫做toString()的方法,如果你只是想打印所有的类属性,使用它会更好。

public class Student
{
    private String name;
    private int studentNumber;
    private String course;

  public String toString(){ 
   return name + " " + studentNumber + " " + course;   
 }  
}

//In your TestStudent class or any other class where you will use Student you can just do it like so:
system.out.println(s1)
//Output: James 1111 Computer Science

最后,这段代码s1.showStudentInformation();不会打印任何东西,因为你没有像system.out.println(s1.show..)那样将它 Package 在一个print中。它只会返回你指定的字符串,但不会打印它。记住,如果你想输出一些东西,总是在system.out.println中完成它。

相关问题