各位大哥:
我在编译 一对多问题是报错:
想要的结果是:南京大学
南京大学
|- 学生姓名:小王 年龄:28 学校:南京大学
|- 学生姓名:大哥 年龄:36 学校:南京大学
|- 学生姓名:好人 年龄:25 学校:南京大学可是出来的是:
南京大学
C04.School@5224ee
|- 学生姓名:小王 年龄:28 学校:C04.School@5224ee
|- 学生姓名:大哥 年龄:36 学校:C04.School@5224ee
|- 学生姓名:好人 年龄:25 学校:C04.School@5224ee
不知道是什么原因啊?package C04;import java.util.List;
import java.util.ArrayList;
import java.util.Iterator;class Student {
private String name;
private int age;
private School school;
public Student(String name,int age){
this.setName(name);
this.setAge(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 +" 学校:" + this.school;
}
}class School{
private String name;
private List<Student> allStudent;
public School(){
this.allStudent = new ArrayList<Student>();
}
public School(String name){
this();
this.setName(name);
}
public String getName(){
return name;
}
public void setName(String name){
this.name = name;
}
public List<Student> getAllStudent(){
return allStudent;
}
}
public class TestDemo01 {
public static void main(String[] args){
School sch = new School("南京大学");
Student st1 = new Student("小王",28);
Student st2 = new Student("大哥",36);
Student st3 = new Student("好人",25);
System.out.println(sch.getName());
sch.getAllStudent().add(st1);
sch.getAllStudent().add(st2);
sch.getAllStudent().add(st3);
st1.setSchool(sch);
st2.setSchool(sch);
st3.setSchool(sch);
System.out.println(sch);
Iterator<Student> iter = sch.getAllStudent().iterator();
while (iter.hasNext()){
System.out.println("\t|- "+iter.next());
}
}}
我在编译 一对多问题是报错:
想要的结果是:南京大学
南京大学
|- 学生姓名:小王 年龄:28 学校:南京大学
|- 学生姓名:大哥 年龄:36 学校:南京大学
|- 学生姓名:好人 年龄:25 学校:南京大学可是出来的是:
南京大学
C04.School@5224ee
|- 学生姓名:小王 年龄:28 学校:C04.School@5224ee
|- 学生姓名:大哥 年龄:36 学校:C04.School@5224ee
|- 学生姓名:好人 年龄:25 学校:C04.School@5224ee
不知道是什么原因啊?package C04;import java.util.List;
import java.util.ArrayList;
import java.util.Iterator;class Student {
private String name;
private int age;
private School school;
public Student(String name,int age){
this.setName(name);
this.setAge(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 +" 学校:" + this.school;
}
}class School{
private String name;
private List<Student> allStudent;
public School(){
this.allStudent = new ArrayList<Student>();
}
public School(String name){
this();
this.setName(name);
}
public String getName(){
return name;
}
public void setName(String name){
this.name = name;
}
public List<Student> getAllStudent(){
return allStudent;
}
}
public class TestDemo01 {
public static void main(String[] args){
School sch = new School("南京大学");
Student st1 = new Student("小王",28);
Student st2 = new Student("大哥",36);
Student st3 = new Student("好人",25);
System.out.println(sch.getName());
sch.getAllStudent().add(st1);
sch.getAllStudent().add(st2);
sch.getAllStudent().add(st3);
st1.setSchool(sch);
st2.setSchool(sch);
st3.setSchool(sch);
System.out.println(sch);
Iterator<Student> iter = sch.getAllStudent().iterator();
while (iter.hasNext()){
System.out.println("\t|- "+iter.next());
}
}}
解决方案 »
- 一道java面试题,请帮我解答一下。问题是:六个国家进行比赛,请你安排5场比赛,不能重复!详情如下:
- 100急求解!
- 怎么将一条数据插入到二张表中
- 一个关于Struts的问题
- 急!!!使用Java如何控制windows 服务的停止和启动??????
- structs 中怎么做一个forward可以指向其他网站,比如www.csdn.net?
- 请问哪位知道关闭WEB浏览器的命令是什么?
- 请教一个SOAP的问题。
- Enterprise JavaBeans Distilled: EJB 2.0规范研究、学习
- 已经j2eeadmin -addJdbcDriver加载驱动,但是还是报错找不到合适驱动!
- 基于J2EE的WEB日志管理系统
- 讨论大学生找工作
你试试这样行不
public class TestDemo01 {
public static void main(String[] args){
School sch = new School("南京大学");
Student st1 = new Student("小王",28);
Student st2 = new Student("大哥",36);
Student st3 = new Student("好人",25); System.out.println(sch.getName()); sch.getAllStudent().add(st1);
sch.getAllStudent().add(st2);
sch.getAllStudent().add(st3); st1.setSchool(sch);
st2.setSchool(sch);
st3.setSchool(sch);
System.out.println(sch.getName()); Iterator <Student> iter = sch.getAllStudent().iterator();
while (iter.hasNext()){
System.out.println("\t|- "+iter.next().getName());
} } }
iter.next() 所指的也是一个School对象
我的Student 类和School 类不是public 的,但是我不知道怎么才能在Myeclipse 中把两个类编译呀?
请大家帮帮忙!
谢谢你!
我有测试了一下,
也不是public 的问题,
是我在School中加了toString 就可以了!