• java:练习学校学生


    java:练习学校学生

    一个学生对应一个学校

    一个学校对应多个学生

    Student类,School类,Demo测试类

    Student:

    public class Student {
    	
    	private String name;
    	private int age;
    	private School school;
    	
    	
    	
    	
    	
    	
    	public Student() {
    		super();
    	}
    
    	public Student(String name, int age) {
    		
    		this.name = name;
    		this.age = age;
    		
    	}
    
    	public String getName() {
    		return name;
    	}
    	
    	public void setName(String name) {
    		this.name = name;
    	}
    	
    	public int getAge() {
    		return age;
    	}
    	
    	public void setAge(int age) {
    		this.age = age;
    	}
    
    	public School getSchool() {
    		return school;
    	}
    
    	public void setSchool(School school) {
    		this.school = school;
    	}
    	
    	
    	public String toString()
    	{
    		return "学生姓名:"+this.name+",学生年龄"+this.age;
    	}
    	
    	
    
    }
    

      

    School类

    public class School {
    
    	private String name;
    	private List<Student> allStudents;
    	
    	
    	public School()
    	{
    		this.allStudents = new ArrayList<Student>();
    	}
    	
    	public School(String name)
    	{
    		this();
    		this.name = name;
    	}
    	
    	public String getName() {
    		return name;
    	}
    	
    	public void setName(String name) {
    		this.name = name;
    	}
    	
    	public List<Student> getAllStudents() {
    		return allStudents;
    	}
    	
    	
    	public String toString()
    	{
    		return "学校信息:" + this.name;
    	}
    	
    	
    	
    	
    }
    

      

    测试;

    //一个学生对应一个学校
    		//一个学校对应多个学生
    		
    		School school = new School("zhdzdx");
    		Student stu1 = new Student("张三",22);
    		Student stu2 = new Student("李四",33);
    		Student stu3 = new Student("王五",22);
    		school.getAllStudents().add(stu1);
    		stu1.setSchool(school);
    		school.getAllStudents().add(stu2);
    		stu2.setSchool(school);
    		school.getAllStudents().add(stu3);
    		stu3.setSchool(school);
    		System.out.println(school);
    		Iterator  iter = school.getAllStudents().iterator();
    		while(iter.hasNext())
    		{
    			Student stu = (Student) iter.next();
    			System.out.println(stu);
    		}
    

      

  • 相关阅读:
    .NET Task揭秘(一)
    .net线程池内幕
    Branch 向量化
    让你的gdb print 更可读
    获取web项目的绝对路径的方法总结
    Android事件监听(一)——简介篇
    Android事件监听(二)——点击鼠标事件
    jsp运行环境的安装和配置
    log4j中的DailyRollingFileAppender日志输出器工作原理
    开发环境搭建
  • 原文地址:https://www.cnblogs.com/achengmu/p/7679377.html
Copyright © 2020-2023  润新知