我们如何在其外部类中访问内部类的非静态成员?

时间:2015-01-16 11:42:30

标签: java inner-classes member-access

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();
    }
像上面的班级学生一样,有一个内部课程科目包含学科名称的信息和学生所拥有的相应标记,如果我想打印所有学科名称和相应的标记以及学生的详细信息,我们怎么能这样做?

0 个答案:

没有答案