private List<Student> studentList;什么意思
时间: 2023-08-22 08:08:00 浏览: 170
private List<Student> studentList; 是一行代码,表示定义了一个私有的列表(List),列表中的元素类型为Student。
具体解释如下:
- private:表示该列表是私有的,只能在类内部访问,无法被外部类或对象直接访问。
- List<Student>:表示该列表中的元素类型为Student,即只能存储Student对象。
- studentList:表示列表的名称,可以根据需要自定义命名。
通过这行代码,我们可以创建一个私有的列表,用于存储学生对象。例如,可以使用以下代码来实例化并操作该列表:
```
List<Student> studentList = new ArrayList<>(); // 创建一个空的学生列表
Student student1 = new Student("John", 20); // 创建一个学生对象
studentList.add(student1); // 将学生对象添加到列表中
Student student2 = new Student("Jane", 22);
studentList.add(student2);
// 遍历列表中的学生对象
for (Student student : studentList) {
System.out.println(student.getName() + ", " + student.getAge());
}
```
上述代码中,我们创建了一个名为studentList的列表,用于存储学生对象。然后,我们创建了两个学生对象,并通过add()方法将它们添加到列表中。最后,通过for-each循环遍历列表中的学生对象,并打印出每个学生的姓名和年龄。