【问题标题】:How can I display an object in java? [duplicate]如何在java中显示对象? [复制]
【发布时间】:2021-01-12 10:20:52
【问题描述】:

我的任务是显示对象 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;
    }
 }
}

当我打印它时,会得到这样的输出:

学生一:StudentTester$student@ba4d54,学生二:StudentTester$student@12bc6874

【问题讨论】:

标签: java class object integer


【解决方案1】:

您应该使用对象访问字段

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

【讨论】:

    猜你喜欢
    • 2013-11-08
    • 2010-10-26
    • 2016-10-02
    • 2011-07-25
    • 1970-01-01
    • 2013-01-13
    相关资源
    最近更新 更多