按照一定步骤执行方法,当时步骤内的方法可能发生修改或添加。
步骤
<pre>
public abstract class Module {
private static final String TAG = "Module";
protected void aVoid() {
Log.i(TAG, "开始: ");
}
protected void bVoid() {
Log.i(TAG, "加速: ");
}
protected void cVoid() {
Log.i(TAG, "减速: ");
}
protected void dVoid() {
Log.i(TAG, "结束: ");
}
public final void running() {
Log.i(TAG, "running: 跑步---------------------");
aVoid();
bVoid();
cVoid();
dVoid();
Log.i(TAG, "running: 跑步------------------------");
}
</pre>
A
<pre>
public class ModuleImplA extends Module {
private static final String TAG = "ModuleImplA";
@Override
protected void aVoid() {
Log.i(TAG, "ModuleImplA: 提前开始");
}
}
</pre>
B
<pre>
public class ModuleImplB extends Module {
private static final String TAG = "ModuleImplB";
@Override
protected void dVoid() {
Log.i(TAG, "ModuleImplB: 提前结束");
}
}
</pre>
使用
<pre>
public class MainActivity extends AppCompatActivity {
private static final String TAG = "MainActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Module module = new ModuleImplA();
module.running();
module = new ModuleImplB();
module.running();
module = new Module() {
@Override
protected void aVoid() {
super.aVoid();
Log.i(TAG, "MainActivity: 重新开始 ");
}
};
module.running();
}
</pre>
log
<pre>
03-13 21:44:58.820 26327-26327/com.lerz.moduledmeo I/Module: running: 跑步---------------------
03-13 21:44:58.820 26327-26327/com.lerz.moduledmeo I/ModuleImplA: ModuleImplA: 提前开始
03-13 21:44:58.820 26327-26327/com.lerz.moduledmeo I/Module: 加速:
03-13 21:44:58.820 26327-26327/com.lerz.moduledmeo I/Module: 减速:
03-13 21:44:58.820 26327-26327/com.lerz.moduledmeo I/Module: 结束:
03-13 21:44:58.820 26327-26327/com.lerz.moduledmeo I/Module: running: 跑步------------------------
03-13 21:44:58.820 26327-26327/com.lerz.moduledmeo I/Module: running: 跑步---------------------
03-13 21:44:58.820 26327-26327/com.lerz.moduledmeo I/Module: 开始:
03-13 21:44:58.820 26327-26327/com.lerz.moduledmeo I/Module: 加速:
03-13 21:44:58.820 26327-26327/com.lerz.moduledmeo I/Module: 减速:
03-13 21:44:58.820 26327-26327/com.lerz.moduledmeo I/ModuleImplB: ModuleImplB: 提前结束
03-13 21:44:58.820 26327-26327/com.lerz.moduledmeo I/Module: running: 跑步------------------------
03-13 21:44:58.820 26327-26327/com.lerz.moduledmeo I/Module: running: 跑步---------------------
03-13 21:44:58.820 26327-26327/com.lerz.moduledmeo I/Module: 开始:
03-13 21:44:58.820 26327-26327/com.lerz.moduledmeo I/MainActivity: MainActivity: 重新开始
03-13 21:44:58.820 26327-26327/com.lerz.moduledmeo I/Module: 加速:
03-13 21:44:58.820 26327-26327/com.lerz.moduledmeo I/Module: 减速:
03-13 21:44:58.820 26327-26327/com.lerz.moduledmeo I/Module: 结束:
03-13 21:44:58.820 26327-26327/com.lerz.moduledmeo I/Module: running: 跑步------------------------
</pre>