做一个QQ分组的小程序:首先新建一个好友类,定义它的属性(QQ号,姓名,个性签名等)。然后定义组类,定义它的属性(组号,组名,管理该组成员列表集合)。最后通过测试类进行测试。

 

 

好友类

public class Buddy {

 private String qqId;
 private String qqName;
 private String qqImpresa;
 public Buddy(String qqId,String qqName,String qqImpresa){
  this.setQqId(qqId);
  this.setQqName(qqName);
  this.setQqImpresa(qqImpresa);
 }
 
 public String getQqId() {
  return qqId;
 }
 public void setQqId(String qqId) {
  this.qqId = qqId;
 }
 public String getQqName() {
  return qqName;
 }
 public void setQqName(String qqName) {
  this.qqName = qqName;
 }
 public String getQqImpresa() {
  return qqImpresa;
 }
 public void setQqImpresa(String qqImpresa) {
  this.qqImpresa = qqImpresa;
 }

 
 
}


 

组类

import java.util.ArrayList;
import java.util.List;


public class Group {

 private String id;
 private String groupName;
 private List<Buddy> buddyList;
 public Group(String id,String groupName){
  this.setId(id);
  this.setGroupName(groupName);
  this.setBuddyList(new ArrayList<Buddy>());
 }
 public void addBuddy(Buddy buddy){
  this.buddyList.add(buddy);
 }
 
    public String getId() {
  return id;
 }
 public void setId(String id) {
  this.id = id;
 }
 public String getGroupName() {
  return groupName;
 }
 public void setGroupName(String groupName) {
  this.groupName = groupName;
 }

 public List<Buddy> getBuddyList() {
  return buddyList;
 }
 public void setBuddyList(List<Buddy> buddyList) {
  this.buddyList = buddyList;
 }
 
 

 
 
}


 

测试类


import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;


public class TestGroup {

 
 public static void main(String[] args) {
  
  List<Group> gList=new ArrayList<Group>();
  
  Group g1=new Group("111","同学");
  Group g2=new Group("222","朋友");
  Group g3=new Group("333","同事");
  gList.add(g1);
  gList.add(g2);
  gList.add(g3);
  
  Buddy hans=new Buddy("01","hans","I am well");
  Buddy Tom=new Buddy("02","Tom","I am happy");
  Buddy Lily=new Buddy("03","Lily","I am fine");
  
  g1.addBuddy(Lily);
  g2.addBuddy(Tom);
  g3.addBuddy(hans);
  
  Iterator<Group> git=gList.iterator();
  while(git.hasNext()){
   Group p=git.next();
   System.out.println("-"+p.getGroupName());
  
  List<Buddy> bList=p.getBuddyList();
  Iterator<Buddy> bit=bList.iterator();
  while(bit.hasNext()){
   System.out.println("|--"+bit.next().getQqName());
  }
  
  }
 }

}


 

运行结果