1、下面代码可以运行。一对多,一个人可以有多个Email地址:
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;class Person{
private String name;
private int age;
public List allEmail;
//这一句,是重点。
public Person(){
this.allEmail=new ArrayList();
}
public Person(String name,int age){
this();
this.setAge(age);
this.setName(name); }
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 List getAllEmail() {
return allEmail;
} public void setAllEmail(List allEmail) {
this.allEmail = allEmail;
}
public String toString(){
return "名字:"+this.name+", 年龄"+this.age;
}
}class Email{
private String url;
private String userName;
private String flag;
public Email(String url,String userName,String flag){ this.setUrl(url);
this.setUserName(userName);
this.setFlag(flag);
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getFlag() {
return flag;
}
public void setFlag(String flag) {
this.flag = flag;
}
public String toString(){
return "url:"+this.url+", 名字:"+this.userName+", flag:"+this.flag;
}
}public class TestJH {
public static void main(String[] args){
Person p=new Person("jack",25);
Email e1=new Email("www.163.com","jackandlucy","@Gmail.com");
Email e2=new Email("www.263.com","jackandlucy","@Gmail.com");
Email e3=new Email("www.363.com","jackandlucy","@Gmail.com");
//这一步很重要,目的是人可以得到哪些,自己的Email地址、
p.getAllEmail().add(e1);
p.getAllEmail().add(e2);
p.getAllEmail().add(e3);
System.out.println(p+" 他有哪些Email:");
List all=p.getAllEmail();
Iterator ite= all.iterator(); while(ite.hasNext()){
Email e=(Email)ite.next();
System.out.println(e);
}
}}
2、下面是多对多,一个学生有多个老师,一个老师有多个学生。代码可以运行。
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;class Student{
private String name;
public List allTea;
public Student(){
this.allTea=new ArrayList();
}
public Student(String name){
this();
this.name=name;
}
public String getName() {
return name;
} public void setName(String name) {
this.name = name;
}
public List getAllTea(){
return this.allTea;
}
public String toString(){
return "学生的名字:"+this.name;
}
}class Teacher{
private int sal;
public List allStu;
public Teacher(){
this.allStu=new ArrayList();
}
public Teacher(int sal){
this();
this.sal=sal;
} public int getSal() {
return sal;
} public void setSal(int sal) {
this.sal = sal;
}
public List getStu(){
return this.allStu;
}
public String toString(){
return "老师的薪水是:"+this.sal;
}
}public class TestMoseToMose {
public static void main(String[] args){
Student st=new Student("jack");
Teacher tc1=new Teacher(12);
Teacher tc2=new Teacher(234);
Teacher tc3=new Teacher(567);
st.getAllTea().add(tc1);
st.getAllTea().add(tc2);
st.getAllTea().add(tc3);
System.out.println(st+" 他有哪些老师:");
Iterator ite1=st.getAllTea().iterator();
while(ite1.hasNext()){
Teacher t =(Teacher)ite1.next();
System.out.println(t);
}
Teacher te=new Teacher(12);
Student st1=new Student("jack");
Student st2=new Student("lucy");
Student st3=new Student("david");
te.getStu().add(st1);
te.getStu().add(st2);
te.getStu().add(st3);
System.out.println(te+" 他有哪些学生:");
Iterator it=te.getStu().iterator();
while(it.hasNext()){
Student s=(Student)it.next();
System.out.println(s);
}
}}
3、不难,运行一下看看。