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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 位雪 中级黑马   /  2012-8-19 11:28  /  1836 人查看  /  2 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 位丹丹 于 2012-8-20 23:50 编辑

关于模版方法设计模式的疑问
  1. abstract class GetTime//此类是获取某段代码运行的时间
  2. {
  3. public void getTime()
  4. {
  5. long start = System.currentTimeMillis();

  6. /*for(int x = 0;x<1000;x++)
  7. {
  8. System.out.print(x);
  9. }*/
  10. runcode();
  11. long end = System.currentTimeMillis();
  12. System.out.println("毫秒:"+(end-start));
  13. }
  14. public abstract void runcode();

  15. /*
  16. * 程序运行代码未知,只是获取时间的一种方式
  17. {
  18. for(int x=0;x<1000;x++)
  19. {
  20. System.out.println(x);
  21. }
  22. }*/

  23. }
  24. class SubTime extends GetTime
  25. {
  26. /*public void getTime()
  27. {
  28. long start = System.currentTimeMillis();

  29. for(int x = 0;x<4000;x++)
  30. {
  31. System.out.print(x);
  32. }
  33. long end = System.currentTimeMillis();
  34. System.out.println("毫秒:"+(end-start));
  35. }*/

  36. //子继父类,只需要复写父类功能定义自己的特有功能
  37. public void runcode()
  38. {
  39. for(int x = 0;x<4000;x++)
  40. {
  41. System.out.print(x);
  42. }
  43. }
  44. }
  45. public class TemplateDemo {

  46. public static void main(String[] args) {
  47. //GetTime gt = new GetTime();
  48. SubTime gt = new SubTime();
  49. gt.getTime();

  50. }

  51. }
  52. 疑问:用这个模式能否求出视频运行时间,用代码怎么实现?求助。。。。。
复制代码

评分

参与人数 1技术分 +1 收起 理由
张_涛 + 1

查看全部评分

2 个回复

倒序浏览
  1. package my;

  2. abstract class GetTime// 此类是获取某段代码运行的时间
  3. {
  4.         public void getTime() {
  5.                 long start = System.currentTimeMillis();
  6.                 runcode();
  7.                 long end = System.currentTimeMillis();
  8.                 System.out.println("耗时" + (end - start) + "毫秒:");
  9.         }

  10.         public abstract void runcode();
  11. }

  12. class SubTime extends GetTime {
  13.         public void runcode(){};
  14. }

  15. public class TemplateDemo {

  16.         public static void main(String[] args) {
  17.                 SubTime gt = new SubTime() {
  18.                         @Override
  19.                         public void runcode() {
  20.                               <font color="#ff0000"> System.out.println("程序开始:");
  21.                                 try {
  22.                                         Thread.sleep(3000);
  23.                                 } catch (InterruptedException e) {
  24.                                         e.printStackTrace();
  25.                                 }
  26.                                 System.out.println("程序结束");</font>
  27.                         }
  28.                 };
  29.                 gt.getTime();
  30.         }
  31. }
复制代码
把主方法里的红色部分改成自己向运行的代码..
就可以统计了.这个使用的是内部类.重写runcode的方法.

点评

多谢  发表于 2012-8-20 23:50

评分

参与人数 1技术分 +1 收起 理由
张_涛 + 1 赞一个!

查看全部评分

回复 使用道具 举报
模版方法设计模式
    规定一个体系,但是其中有的一部分需要用户自己去编写,其他的按部就班,这个体系就叫做模版设计模式,例如:一个类有一个模版,执行顺序是第一、第二 、第三 ,但是不知道第二怎么去执行,把这部分抽象出来,那个这个类也是抽象类了。子类在继承这个类时,只要实现第二个方法就可以了。其他的不做任何改变。

策略设计模式
  自定义一个类,但是所有的功能都已经完成,返回结果是,不知道用户需要怎么样的结果,这个参数由用户调用这个类是用户自己传递,而自定义类时,只是将这个参数封装成一个接口,用户自己去实现,自己去定义返回什么样的结果。

点评

请通过板块上面的改名通道进行改名,以免影响加分!  发表于 2012-8-21 06:09

评分

参与人数 1技术分 +1 收起 理由
张_涛 + 1 新人回帖,赞一个!

查看全部评分

回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马