java 贴吧代码_【图片】JAVA代码【java吧】_百度贴吧

该楼层疑似违规已被系统折叠 隐藏此楼查看此楼

public class Student{

private String name ;

private int age ;

private School school; // 一个学生属于一个学校

public Student(String name,int age){

this.setName(name) ;

this.setAge(age) ;

}

public void setSchool(School school){

this.school= school ;

}

public School getSchool(){

return this.school;

}

public void setName(String name){

this.name= name ;

}

public void setAge(int age){

this.age= age ;

}

public String getName(){

return this.name;

}

public int getAge(){

return this.age;

}

public String toString(){

return "学生姓名:" + this.name+ ";年龄:" + this.age;

}

};

import java. util. *;

public class School{

private String name ;

private List allStudents ;

public School(){

this.allStudents= new ArrayList() ;

}

public School(String name){

this() ;

this.setName(name) ;

}

public void setName(String name){

this.name= name ;

}

public String getName(){

return this.name;

}

public List getAllStudents(){

return allStudents;

}

public String toString(){

return "学校:" + this.name;

}

}

import java. util. *;

public class TestDemo{

public static void main(String args[]){

School sch = new School("清华大学") ; // 定义学校

Student s1 = new Student("张三",21) ;

Student s2 = new Student("李四",22) ;

Student s3 = new Student("王五",23) ;

sch.getAllStudents().add(s1) ;

sch.getAllStudents().add(s2) ;

sch.getAllStudents().add(s3) ;

s1.setSchool(sch) ;

s2.setSchool(sch) ;

s3.setSchool(sch) ;

System.out.println(sch) ;

Iterator iter = sch.getAllStudents().iterator() ;

while(iter.hasNext()){

System.out.println("\t|- " + iter.next()) ;

}

}

};

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值