|
常用链接
留言簿(2)
随笔档案
文章档案
搜索
最新评论
阅读排行榜
评论排行榜
Powered by: 博客园
模板提供:沪江博客
|
|
|
|
|
发新文章 |
|
|
我们做一个QQ分层的一块:那就是给好友列表分组。
我们知道:我们的QQ里面分了好几个组:好友,同学,家人,陌生人,黑名单等等,那么这是怎么实现的呢?
首先,我们需要有一群好友(好友类),他们有各种各样的属性(QQ号,姓名,签名,在线状态),非常多,十分庞杂;这个时候我们需要给他们分组了(组类),每个组里面也有这个组的属性(组号,组名,管理该组成员的列表集合),每个组都有基本功能就是增加该组的成员和删除该组的成员。
最后通过测试类,我们看看管理分组的运行情况。
下面附代码:
好友类:
public class Buddy {
private int qq;
private String name;
private String sign;
private int status;
public Buddy(int qq,String name,String sign,int status){
this.setQq(qq);
this.setName(name);
this.setSign(sign);
this.setStatus(status);
}
public int getQq() {
return qq;
}
public void setQq(int qq) {
this.qq = qq;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSign() {
return sign;
}
public void setSign(String sign) {
this.sign = sign;
}
public int getStatus() {
return status;
}
public void setStatus(int status) {
this.status = status;
}
public String toString(){
if(this.getStatus()==1){
return "QQ号:"+this.getQq()+" 好友名:"+this.getName()+" 好友签名:"+this.getSign()+" 好友状态: 在线";
}
else{
return "QQ号:"+this.getQq()+" 好友名:"+this.getName()+" 好友签名:"+this.getSign()+" 好友状态: 离线";
}
}
}
组类:
public class Group {
private int id;
private String name;
private List<Buddy> buddyList;
public Group(int id,String name){
this.setId(id);
this.setName(name);
this.setBuddyList(new ArrayList<Buddy>());
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<Buddy> getBuddyList() {
return buddyList;
}
public void setBuddyList(List<Buddy> buddyList) {
this.buddyList = buddyList;
}
public void addBuddy(Buddy buddy){
this.buddyList.add(buddy);
}
public void removeBuddy(Buddy buddy){
this.buddyList.remove(buddy);
}
}
测试类:
public class Test {
public static void main(String[] args) {
List<Group> grouplist = new ArrayList<Group>();
Group friend = new Group(1,"好友表");
Group family = new Group(2,"家人表");
grouplist.add(friend);
grouplist.add(family);
Buddy b1 = new Buddy(001,"张三","我很好啊",0);
Buddy b2 = new Buddy(002,"李四","今天吃饱了",1);
Buddy b3 = new Buddy(003,"王五","吃我煎饼果子",1);
Buddy b4 = new Buddy(004,"赵六","我只吃了臭豆腐",0);
Buddy b5 = new Buddy(001,"爸爸","好好干",1);
Buddy b6 = new Buddy(002,"妈妈","今天做的菜好吃",0);
Buddy b7 = new Buddy(003,"表姐","孩子1岁了",1);
Buddy b8 = new Buddy(004,"姑姑","老太太今天高兴",0);
friend.addBuddy(b1);
friend.addBuddy(b2);
friend.addBuddy(b3);
friend.addBuddy(b4);
family.addBuddy(b5);
family.addBuddy(b6);
family.addBuddy(b7);
family.addBuddy(b8);
System.out.println(friend.getName()+"成员有:");
Iterator<Buddy> iter = friend.getBuddyList().iterator();
while(iter.hasNext()){
Buddy b = iter.next();
System.out.println(b);
}
System.out.println(family.getName()+"成员有:");
Iterator<Buddy> iter1 = family.getBuddyList().iterator();
while(iter1.hasNext()){
Buddy b = iter1.next();
System.out.println(b);
}
}
}
看一下运行结果:
|
|