A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© jonn 高级黑马   /  2013-1-15 19:08  /  1054 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

  1. interface InternetService{
  2. public void connect();
  3. public void disconnect();
  4. }
  5. interface MailService{
  6. public void send();
  7. public void receive();
  8. }
  9. interface VirusService{
  10. public void begin();
  11. public void end();
  12. }
  13. class InternetServiceImpl implements InternetService{
  14. public void connect(){
  15. System.out.println("开始连接网络....");
  16. }
  17. public void disconnect(){
  18. System.out.println("网络断开....");
  19. }
  20. }
  21. class MailServiceTmpl implements MailService{
  22. public void send(){
  23. System.out.println("发送邮箱");
  24. }
  25. public void receive(){
  26. System.out.println("接收邮箱");
  27. }
  28. }
  29. class VirusServiceTmpl implements VirusService{
  30. public void begin(){
  31. System.out.println("开始检查病毒");
  32. }
  33. public void end(){
  34. System.out.println(杀毒完毕...");
  35. }
  36. }
  37. interface Menu1 extends InternetService,MailService{

  38. }
  39. interface Menu2 extends InternetSerice,VirusService{
  40. }
  41. class Menu1Impl implements Menu1{
  42. private InternetService is;
  43. private MailService ms;
  44. public MenuImpl(InternetService is,MailService ms){
  45. this.is=is;
  46. tihs.ms=ms;
  47. }
  48. public void connect(){
  49. is.connect();
  50. }
  51. public void disconnect(){
  52. is.disconnect();
  53. }
  54. public void send(){
  55. ms.send();
  56. }
  57. public void receive(){
  58. ms.receive)();
  59. }
  60. }
  61. class Menu2Impl implements Menu2{
  62. private InternetService is;
  63. private VirusService vs;
  64. public Menu2Impl(InternetService is,VirusService vs){
  65. this.is=is;
  66. this.vs=vs;
  67. }
  68. public void connect(){
  69. is.connect();
  70. }
  71. public void disconnect(){
  72. is.disconnect();
  73. }
  74. public void begin(){
  75. vs.begin();
  76. }
  77. public void end(){
  78. vs.end();
  79. }
  80. }
  81. public class ServiceDemo{
  82. public static void main(String[] args){
  83. InternetService is=new InternetServiceImpl();
  84. MailService ms=new MailServiceImpl();
  85. Menu1Impl m1=new MenuImpl(is,ms);
  86. System.out.println("检测menu1的功能中....");
  87. m1.connect();
  88. m1.disconnect();
  89. m1.send();
  90. m1.receive();
  91. }
  92. }
复制代码
在复合接口设计中,如何将两个系统更完好了进行耦合呢

0 个回复

您需要登录后才可以回帖 登录 | 加入黑马