0
public class Student {
    int rollNumber;
    String name;
    int noOfSubjects;
    ArrayList<Subject> subjectList = new ArrayList<>();

    public TestStudent(int rollNumber, String name, int noOfSubjects) {
        super();
        this.rollNumber = rollNumber;
        this.name = name;
        this.noOfSubjects = noOfSubjects;

    }

    // inner class
    public class Subject {
        String subjectName;
        int marks;

        public Subject(String subjectname, int marks) {
            super();
            subjectName = subjectname;
            this.marks = marks;
        }

        public int getSubjectMarks() {
            return marks;

        }

    }

    public void addSubject(Subject s) {
        subjectList.add(s);

    }

    public int getMarks(Subject s) {
        return s.getSubjectMarks();
    }

就像上面的学生类一样,有一个内部类 Subject 包含科目名称和相应分数的信息,所以如果我想打印所有科目名称和相应的分数以及学生的详细信息,我们该怎么做?

4

0 回答 0