1、创建main方法并编写如下代码 2、增加班内的3个学生(姓名和年龄随意) 3、增加班内的3个班委(职位随意) 4、显示学生和班委学习和工作的内容
时间: 2024-11-27 19:08:57 浏览: 14
首先,我们需要在Java程序中创建一个`main`方法作为程序的入口点。下面是一个简单的例子,它会按照您的要求创建一个班级结构,包含学生和班委,并展示他们的基本信息。
```java
public class Main {
public static void main(String[] args) {
// 创建学生列表
List<Student> students = new ArrayList<>();
students.add(new Student("小明", 16));
students.add(new Student("小红", 15));
students.add(new Student("小刚", 17));
// 创建班委列表
List<ClassCommitteeMember> committeeMembers = new ArrayList<>();
ClassCommitteeMember monitor = new ClassCommitteeMember("班长", "张三", 18);
committeeMembers.add(monitor);
ClassCommitteeMember viceMonitor = new ClassCommitteeMember("副班长", "李四", 17);
committeeMembers.add(viceMonitor);
ClassCommitteeMember secretary = new ClassCommitteeMember("学习委员", "王五", 16);
committeeMembers.add(secretary);
// 显示学生信息
for (Student student : students) {
System.out.printf("%s, %d岁%n", student.getName(), student.getAge());
}
// 显示班委信息
for (ClassCommitteeMember member : committeeMembers) {
System.out.printf("%s, %s, %d岁%n", member.getTitle(), member.getName(), member.getAge());
}
}
}
class Student {
private String name;
private int age;
public Student(String name, int age) {
this.name = name;
this.age = age;
}
// getters and setters...
public String getName() { return name; }
public int getAge() { return age; }
}
class ClassCommitteeMember {
private String title;
private String name;
private int age;
public ClassCommitteeMember(String title, String name, int age) {
this.title = title;
this.name = name;
this.age = age;
}
// getters and setters...
public String getTitle() { return title; }
public String getName() { return name; }
public int getAge() { return age; }
}
```
阅读全文