1、
创建一个浴池管理人员,其中有一个方法,对一组学生进行检查,如果是男生,可以进入浴池洗浴,如果是女生,不能进入浴池。
现有如下学生
张三 男
李四 男
王五 女
赵六 男
请管理人员输出,最后检查结果
package com.xzit.model; //学生类 public class Student { private String name; private String sex; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } @Override public String toString() { return "Student{" + "name='" + name + '\'' + ", sex='" + sex + '\'' + '}'; } }
package com.xzit.model; import java.util.Set; //泳池管理员类 public class Manager { private String name; private Set<Student> student; public String getName() { return name; } public void setName(String name) { this.name = name; } public Set<Student> getStudent() { return student; } public void setStudent(Set<Student> student) { this.student = student; } public void checkStudent(){ Set<Student> students = this.getStudent(); for (Student student : students){ if (student.getSex().equals("男")){ System.out.println(name+"允许"+student.getName()+"进入泳池"); }else{ System.out.println(name+"不允许"+student.getName()+"进入泳池"); } } } @Override public String toString() { return "Manager{" + "name='" + name + '\'' + ", student=" + student + '}'; } }
package com.xzit.util; import com.xzit.model.Manager; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClasspathXmlApplicationContext; public class Test { public static void main(String[] args) { ApplicationContext context = new ClasspathXmlApplicationContext("bean.xml"); Manager manager = (Manager) context.getBean("manager"); manager.checkStudent(); } }
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="manager" class="com.xzit.model.Manager"> <property name="name" value="泳池管理员"></property> <property name="student"> <set> <ref bean="a"></ref> <ref bean="b"></ref> <ref bean="c"></ref> <ref bean="d"></ref> </set> </property> </bean> <bean id="a" class="com.xzit.model.Student"> <property name="name" value="张三"></property> <property name="sex" value="男"></property> </bean> <bean id="b" class="com.xzit.model.Student"> <property name="name" value="李四"></property> <property name="sex" value="男"></property> </bean> <bean id="c" class="com.xzit.model.Student"> <property name="name" value="王五"></property> <property name="sex" value="女"></property> </bean> <bean id="d" class="com.xzit.model.Student"> <property name="name" value="赵六"></property> <property name="sex" value="男"></property> </bean> </beans>