java 如何在Java中显示对象的内容
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25880618/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to display contents of an object in Java
提问by Neo
Ok, now I think I've given up all hope of finding solution to what should to be a simple problem. Basically, I'm creating a students' record system that stores students' details in an ArrayList. I first created a constructor in the Student class to specify what entries each student will have. I then created an instance of the Student class in the main class (i.e. class with the main method) and then added the student object to the studentList ArrayList.
好的,现在我想我已经放弃了找到解决应该是一个简单问题的方法的所有希望。基本上,我正在创建一个学生记录系统,将学生的详细信息存储在 ArrayList 中。我首先在 Student 类中创建了一个构造函数来指定每个学生将拥有的条目。然后我在主类中创建了一个Student 类的实例(即带有main 方法的类),然后将student 对象添加到studentList ArrayList 中。
By the way, instead of hard-coding the student details, my initial aim was to let the user enter the details and then I'll use a Scanner or BufferedReader object to get the details stored in the Student object, and then to the ArrayList but I'm having trouble with that as well; so I'll probably tackle that problem as soon as I'm done with this one.
顺便说一句,我最初的目标不是对学生详细信息进行硬编码,而是让用户输入详细信息,然后我将使用 Scanner 或 BufferedReader 对象来获取存储在 Student 对象中的详细信息,然后到 ArrayList但我也遇到了麻烦;所以我可能会在完成这个问题后尽快解决这个问题。
Anyway, I'm expecting the output to print out the students' details but instead I get a memory location (i.e. [studentrecordsys.Student@15c7850]). I'm aware that I need to override the toString method but how exactly this is done is what I can't seem to get. I get syntax errors everywhere as soon as I enter the @Override code block for the toString method. Here's what I've tried:
无论如何,我期待输出打印出学生的详细信息,但我得到了一个内存位置(即 [studentrecordsys.Student@15c7850])。我知道我需要覆盖 toString 方法,但是我似乎无法理解这是如何完成的。输入 toString 方法的@Override 代码块后,到处都会出现语法错误。这是我尝试过的:
import java.util.*;
class Student {
private String studentID;
private String studentName;
private String studentAddress;
private String studentMajor;
private int studentAge;
private double studentGPA;
Student (String studentID, String studentName, String studentAddress, String
studentMajor, int studentAge, double studentGPA){
this.studentID=studentID;
this.studentName=studentName;
this.studentAddress=studentAddress;
this.studentMajor=studentMajor;
this.studentAge=studentAge;
this.studentGPA=studentGPA;
}
}
public static void main(String[] args) {
Student ali = new Student("A0123", "Ali", "13 Bond Street", "BSc Software Engineering", 22, 3.79);
List<Student> studentList = new ArrayList<>();
studentList.add(ali);
@Override
String toString() {
StringBuilder builder = new StringBuilder();
builder.append(ali).append(studentList);
return builder.toString();
}
System.out.println(builder);
}
回答by Kick Buttowski
You to override toString
method because it is going to give you clear information about the object in readable format that you can understand.
您要覆盖toString
方法,因为它将以您可以理解的可读格式为您提供有关对象的清晰信息。
The merit about overriding toString
:
关于覆盖的优点toString
:
Help the programmer for loggingand debugging of Java program
Since toStringis defined in java.lang.Objectand does not give valuable information, so it is good practice to override it for subclasses.
帮助程序员记录和调试Java程序
由于toString是在java.lang.Object 中定义的并且不提供有价值的信息,因此最好为子类覆盖它。
Source and Read more about overriding toString
public String toString() {
return "Studnet ID: " + this.studentID + ", Student Name:"
+ this.studentName+ ", Studnet Address: " + this.studentAddress
+ "Major" + this.studentMajor + "Age" + this.studentAge
+ GPA" + this.studentGPA ;
}
回答by daentech
You need to implement the toString() on the Student object.
您需要在 Student 对象上实现 toString() 。
public class Student {
...
Your existing code
...
@Override
public String toString() {
return studentID + ", " + studentName + ", " + //The remaining fields
}
}
then in your main method, call
然后在你的主要方法中,调用
for (Student student : studentList) {
System.out.println(student.toString());
}
回答by qbit
You get errors because you have to Override the toString
method inside the class you want to use it for. i.e you have to put the method, with the @Override inside your Student
class.
您会收到错误,因为您必须覆盖toString
要使用它的类中的方法。即你必须把方法,@Override 放在你的Student
类中。
And you can call it like this:
你可以这样称呼它:
System.out.println(studentA.toString());
System.out.println(studentB.toString());
or in a loop:
或在循环中:
for(Student x : studentList)
System.out.println(x.toString());
and so on..
等等..
Also, in your code you create a method inside your main
method. Of course you will get errors.
此外,在您的代码中,您在方法中创建了一个main
方法。当然你会得到错误。