Application of polymorphism: TemplateMethod design pattern
Abstract class is a kind of template pattern design, abstract class as a general template of multiple subclasses, subclasses in the abstract class on the basis of the extension, transformation, the same part in the parent class, different parts of the abstract method, by the subclass to implement, but the subclass will retain the behavior of the abstract class.
Problems solved:
1. When some of the internal implementation of a function is certain and some of the implementation is uncertain, it is possible to expose the uncertain parts and let subclasses implement them
2. In other words, when implementing an algorithm in software development, the overall steps are fixed and generic. The steps are already written in the parent class, but some parts are mutable, and the mutable parts can be abstracted out for different subclasses to implement
Public void runTime(){public void runTime(){public void runTime(){public void runTime(); This.runcode (); this.runcode (); Long end= system.currentTimemillis (); System.out.println(" time :"+(end-start)); } public abstract void runCode(); Class Computer2 extends SuperClass{@override public void runCode(){for(int I =0; i<1000; i++){ if(i%2==0){ System.out.println(i); }}}} class Computer extends SuperClass{@override public void runCode(){for(int I =0; i<10000; i++){ System.out.println(i); Public class TemplateMethodTest {public static void main(String[] args) {SuperClass sc=new Computer2(); sc.runTime(); }}Copy the code