类图
源码
AbChatroom.java package com.demo; public abstract class AbChatroom { public abstract void register(Member m); public abstract void sendtext(String a,String b,String mess); public abstract void sendimage(String a,String b,String mess); } Diamondmember.java package com.demo; public class Diamondmember extends Member{ public Diamondmember(String a){ super(a); } public void sendtext(String a,String c){ System.out.println("钻石会员发送消息"); room.sendtext(name,a,c); } public void sendimage(String a,String c){ System.out.println("钻石会员发送图片"); room.sendimage(name,a,c); } } Group.java package com.demo; import java.util.Hashtable; public class Group extends AbChatroom{ private Hashtable ms=new Hashtable(); public void register(Member m) { if(!ms.contains(m)){ ms.put(m.getname(),m); m.setroom(this); } } public void sendtext(String a,String b,String c){ Member m=(Member)ms.get(b); String newm=c; newm=c.replaceAll("日","*"); m.receivetext(a,newm); } public void sendimage(String a,String b,String c){ Member m=(Member)ms.get(b); if(c.length()>5){ System.out.println("图片较大,发送失败"); }else{ m.receiveimage(a,c); } } } Member.java package com.demo; public abstract class Member { protected AbChatroom room; protected String name; public Member(String a){ name=a; } public String getname(){ return name; } public void setname(String a){ name=a; } public AbChatroom getroom(){ return room; } public void setroom(AbChatroom a){ room=a; } public abstract void sendtext(String a,String mess); public abstract void sendimage(String a,String image); public void receivetext(String a,String mess){ System.out.println(a+"发送的给"+name+"内容是"+mess); } public void receiveimage(String a,String image){ System.out.println(a+"发送的给"+name+"内容是"+image); } } Newmember.java package com.demo; public class Newmember extends Member{ public Newmember(String a){ super(a); } public void sendtext(String a,String c){ System.out.println("新会员发送消息"); room.sendtext(name,a,c); } public void sendimage(String a,String c){ System.out.println("新会员不能发送图片"); room.sendimage(name,a,c); } } Ordinary.java package com.demo; public class Ordinary extends Member{ public Ordinary(String a){ super(a); } public void sendtext(String a,String c){ System.out.println("普通会员发送消息"); room.sendtext(name,a,c); } public void sendimage(String a,String c){ System.out.println("普通会员不能发送图片"); } } Room.java package com.demo; import java.util.HashMap; import java.util.Map; public class Room extends AbChatroom{ Map<String,Member> table = new HashMap<>(); public void register(Member member) { table.put(member.getname(), member); member.setroom(this); } public void sendtext(String from, String to, String message) { if(message.length()>=100){ System.err.println("超过100个字符,发送失败"); }else{ Member member = table.get(to); if(member!=null) member.receivetext(from, message); } } public void sendimage(String from, String to, String message) { if(message.length()>=20){ System.err.println("图片超过20M,发送失败"); }else{ Member member = table.get(to); if(member!=null) member.receiveimage(from, message); } } } Client.java package com.demo; public class Client { public static void main(String args[]){ AbChatroom r1=new Room(); Member m1,m2,m3; m1=new Newmember("A"); m2=new Newmember("B"); m3=new Newmember("C"); r1.register(m1); r1.register(m2); r1.register(m3); m1.sendtext("B",":<Message1>"); m2.sendtext("A",":<Message2>"); m2.sendtext("A",":<Message3>"); m3.sendimage("A",":<jpg1>"); m3.sendimage("B",":<jpg2>"); } }
测试截图