没啥说的,这东西有点类似于动态代理。
主代码:
package reflect_study_11;
import java.io.IOException;
public class IoDemo {
public static void main(String[] args) {
GetTime g =new GetTimeDemo();
try {
g.getTime();
} catch (IOException e) {
e.printStackTrace();
}
}
}
调用模版的代码
package reflect_study_11;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class GetTimeDemo extends GetTime{
@Override
void method() throws IOException {
FileInputStream fis=new FileInputStream("配置文件.txt");
FileOutputStream fos=new FileOutputStream("配置文件1.txt");
int by=0;
while((by=fis.read())!=-1)
{
fos.write(by);
}
fis.close();
fos.close();
}
}
模版代码:
package reflect_study_11;
import java.io.IOException;
public abstract class GetTime {
public void getTime() throws IOException {
long star1 = System.currentTimeMillis();
method();
long end1 =System.currentTimeMillis();
System.out.println("花费的时间是"+(star1-end1));
}
abstract void method() throws IOException;
}