如何在 java 中显示对象?
How can I display an object in java?
我的任务是显示对象 S1 和 S2 的值变量等级。
我的代码:
public class StudentTester{
public static void main(String[] args){
student S1 = new student();
student S2 = new student();
S1.setGrade(12);
System.out.println("Student one: " +S1+", Student two: "+S2);
}
public static class student {
private String name;
private int age;
private int grade;
private double average;
private boolean disability;
private void printStudentInfo(){
//Data Encapsulation is methods of the public interface provide access to private data, while hiding implementation.
System.out.println("Name: "+name+",Age: "+age+",Grade: "+grade+",Average: "+average +" Disability: "+disability);
}
public void setGrade(int newGrade){
grade=newGrade;
}
public int getGrade(){
return grade;
}
}
}
当我打印它时得到这样的输出:
Student one: StudentTester$student@ba4d54, Student two: StudentTester$student@12bc6874
您应该使用对象访问字段
public static void main(String[] args){
student S1 = new student();
student S2 = new student();
S1.setGrade(12);
System.out.println("Student one: " +S1.getGrade()+", Student two: "+S2.getGrade());
}
我的任务是显示对象 S1 和 S2 的值变量等级。
我的代码:
public class StudentTester{
public static void main(String[] args){
student S1 = new student();
student S2 = new student();
S1.setGrade(12);
System.out.println("Student one: " +S1+", Student two: "+S2);
}
public static class student {
private String name;
private int age;
private int grade;
private double average;
private boolean disability;
private void printStudentInfo(){
//Data Encapsulation is methods of the public interface provide access to private data, while hiding implementation.
System.out.println("Name: "+name+",Age: "+age+",Grade: "+grade+",Average: "+average +" Disability: "+disability);
}
public void setGrade(int newGrade){
grade=newGrade;
}
public int getGrade(){
return grade;
}
}
}
当我打印它时得到这样的输出:
Student one: StudentTester$student@ba4d54, Student two: StudentTester$student@12bc6874
您应该使用对象访问字段
public static void main(String[] args){
student S1 = new student();
student S2 = new student();
S1.setGrade(12);
System.out.println("Student one: " +S1.getGrade()+", Student two: "+S2.getGrade());
}