当前位置:网站首页>A small template (an abstract class, a complete process is written in a method, the uncertain part is written in the abstract method, and then the subclass inherits the abstract class, and the subclas
A small template (an abstract class, a complete process is written in a method, the uncertain part is written in the abstract method, and then the subclass inherits the abstract class, and the subclas
2022-06-30 06:17:00 【Java shaping】
When testing :main Function instantiates the object of the parent class , Instantiate objects of subclasses ,( I have polymorphic subclass objects here ) Whatever can be executed is the method of the parent class , Then the template is completed
public class TemplateTest {
public static void main(String[] args) {
Template t = new SubTemplate();
t.spendTime();
}
}
abstract class Template {
public void spendTime() {
// Calculate the time spent executing a piece of code
long start = System.currentTimeMillis();
code();// The uncertain part 、 The volatile part
long end = System.currentTimeMillis();
System.out.println(" The time spent is :" + (end - start));
}
public abstract void code();// Abstract methods , Subclass rewriting
}
class SubTemplate extends Template {
// Subclass rewriting code Method
public void code() {
// Example : Calculation 1000 The prime number within
for (int i = 2; i <= 1000; i++) {
boolean isFlag = true;
for (int j = 2; j < Math.sqrt(i); j++) {
if (i % j == 0) {
isFlag = false;
break;
}
}
if (isFlag) {
System.out.println(i);
}
}
}
}
边栏推荐
- 反編譯正常回編譯出現問題自己解决辦法
- How to use unmarshaljson
- Image processing 7- image enhancement
- Balanced binary tree judgment of Li Kou 110 -- classic problems
- Official win 10 image download
- Problems related to pinduoduo store search, why can't new products be found on the shelves
- Configure the user to log in to the device through telnet -- AAA local authentication
- ES6 deconstruction assignment
- C language code record
- Es6数组
猜你喜欢

SparseArray

Use of observer mode and status mode in actual work

飞升:基于中文分词器IK-2种自定义热词分词器构建方式showcase & 排坑showtime
![[ansible series] fundamentals 02 module debug](/img/99/c53be8e2a42c7cb5b4a9a7ef4ad98c.jpg)
[ansible series] fundamentals 02 module debug

Master slave synchronization of MySQL database to realize read-write separation

MySQL数据库用户管理

Golang's handwritten Web Framework

Intelligent question - horse racing question

关于Glide加载图片模糊不清楚

MySQL事物
随机推荐
SparseArray
圖像處理7-圖像增强
Swoole process model diagram
Beauty of Refactoring: when multithreaded batch processing task lifts the beam - Universal scaffold
Common address collection
[Alibaba cloud] student growth plan answers
01. 正则表达式概述
Strlen and sizeof, array length and string length, don't be silly
Is it safe to open an account online? Can you open an account to speculate on the Internet?
MySQL log management, data backup and recovery
图片。。。。。
01. regular expression overview
从零开发 stylelint规则(插件)
数据读写:Unity中基于C#脚本实现数据读写功能
[ansible series] fundamentals -01
Completabilefuture: from understanding to mastering, here are all you want to know
Jgaurora A8 configuration file
ES6 extended operator (...)
PHP knowledge points
Title: enter two positive integers m and N to find their maximum common divisor and minimum common multiple