java作业,用饿汉模式产生班长,模拟班会举行
时间: 2023-05-29 15:05:16 浏览: 111
public class Monitor {
private static final Monitor instance = new Monitor();
private String name;
private List<Student> students;
private Monitor() {
this.name = "张三";
this.students = new ArrayList<>();
students.add(new Student("李四"));
students.add(new Student("王五"));
students.add(new Student("赵六"));
students.add(new Student("小明"));
students.add(new Student("小红"));
}
public static Monitor getInstance() {
return instance;
}
public void conductMeeting() {
System.out.println("班长" + name + "主持班会:");
for (Student student : students) {
student.listenMeeting();
}
}
}
class Student {
private String name;
public Student(String name) {
this.name = name;
}
public void listenMeeting() {
System.out.println(name + "听取班长讲话");
}
}
class Test {
public static void main(String[] args) {
Monitor monitor = Monitor.getInstance();
monitor.conductMeeting();
}
}
阅读全文