黑马程序员技术交流社区

标题: 关于模板方法运行时间的问题。 [打印本页]

作者: 我为你着迷    时间: 2014-6-8 23:45
标题: 关于模板方法运行时间的问题。
  1. abstract class GetTime
  2. {
  3.                 public final void getTime{}
  4.                 {
  5.                          long start=System.currentTimeeMillis();
  6.                                  
  7.                          runcode();
  8.                                  
  9.                          long end=System.currentTimeMillis();
  10.                                  
  11.                          System.out,println("毫秒:"+(end-start));
  12.            }
  13.           
  14.            public abstract void runcode();
  15. }

  16. class SubTime extends GetTime
  17. {
  18.                 public void runcode()
  19.                 {
  20.                         for(int x=0;x<4000;x++)
  21.                         {
  22.                           System.out.println(x);
  23.                         }
  24.                 }
  25. }

  26. class TemplateDemo
  27. {
  28.                 public static void main(String[] args)
  29.                 {
  30.                         SubTime st=new SubTime();
  31.                
  32.                 }
  33. }
复制代码
有个问题我想不明白。因为getName方法已经被final修饰了,所以子类是不能继承这个方法的了。
那么在建立子类对象的时候该如何去调用才能让代码运行呢? 求大神指点迷津,谢谢!


作者: 雾里看花。    时间: 2014-6-9 09:10
首先 我先纠正一下,  你的标点符号都打错了亲
作者: 123_yaya    时间: 2014-6-9 10:42
直接调用即可,因为子类没有实现父类的方法,调用父类的方法的时候,直接用的就是父类的方法。另外你程序很多小细节打错了,改了一下,直接调用能运行的。
  1. package com.study.two;

  2. abstract class GetTime
  3. {
  4.                 public final void getTime()
  5.                 {
  6.                          long start=System.currentTimeMillis();
  7.                                  
  8.                          runcode();
  9.                                  
  10.                          long end=System.currentTimeMillis();
  11.                                  
  12.                          System.out.println("毫秒:"+(end-start));
  13.            }
  14.            
  15.            public abstract void runcode();
  16. }

  17. class SubTime extends GetTime
  18. {
  19.                 public void runcode()
  20.                 {
  21.                         for(int x=0;x<4000;x++)
  22.                         {
  23.                           System.out.println(x);
  24.                         }
  25.                 }
  26. }

  27. class TemplateDemo
  28. {
  29.                 public static void main(String[] args)
  30.                 {
  31.                         SubTime st=new SubTime();
  32.                         st.getTime();
  33.                
  34.                 }
  35. }
复制代码

作者: superob123    时间: 2014-6-9 11:50
直接用就可以了
作者: 我为你着迷    时间: 2014-6-9 12:58
雾里看花。 发表于 2014-6-9 09:10
首先 我先纠正一下,  你的标点符号都打错了亲

哪里  亲  请纠正到底啊 亲
作者: 我为你着迷    时间: 2014-6-9 13:03
123_yaya 发表于 2014-6-9 10:42
直接调用即可,因为子类没有实现父类的方法,调用父类的方法的时候,直接用的就是父类的方法。另外你程序很 ...

首先谢谢你啊  不过哪些细节呢  可不可以指点下啊 {:3_65:}
作者: 123_yaya    时间: 2014-6-9 13:09
我为你着迷 发表于 2014-6-9 13:03
首先谢谢你啊  不过哪些细节呢  可不可以指点下啊

getTime(),应该是括号。
currentTimeMillis这个单词你拼错了。
  for(int x=0;x<4000;x++) 这句你的右括号好像是中文输入的右括号
  System.out,println("毫秒:"+(end-start)); 这句你out . 这个点写成逗号了

楼主要细心点。。
作者: 我为你着迷    时间: 2014-6-9 13:44
123_yaya 发表于 2014-6-9 13:09
getTime(),应该是括号。
currentTimeMillis这个单词你拼错了。
  for(int x=0;x

谢谢你啊 很感动  {:3_64:}




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2