In fact, the application is just a combination of the previously learned knowledge, simulating a very simple project-the printer.
1, first to do a printer printing,
Public class Father { void turn_on () { System.out.println ("Turn on"); } void Turn_off () { System.out.println ("Turn off"); } void Print (String s) { System.out.println ("print" + s);} }
Public class Test { publicstaticvoid Main (String args []) { new father (); P.turn_on (); P.print ("a"); P.turn_off (); }}
This is very simple to realize the power-on, shutdown, printing
2, then increase the number of printers;
1) Use inheritance here, because if a printer write a class, it will produce a lot of duplicate code (no matter what printer has power on, shutdown, print these several functions);
2) Each printer's class writes his own method, if the inherited method in the parent class does not satisfy the requirements, it is rewritten in the subclass, using super to invoke the function in the parent class;
Such as:
// parent class father { void turn_on () {System.out.println (" Turn on "); void Turn_off () {System.out.println ( "Turn off" ); void print (String s) {SYSTEM.OUT.PR Intln ( "print" + s); }}
// printer 1 class hpprint extends Span style= "color: #000000;" > father{} // printer 2 public class canno extends father{ void clean () {System.out.pri Ntln ( "clean" ); void Turn_off () { this .clean (); super .turn_on (); }}
// Main function Public class Test { publicstaticvoid Main (String args []) { new father (); P.turn_on (); P.print ("a"); P.turn_off (); }}
In this way can reduce a lot of duplicate code, imagine, if there are 10 printers, it is more than every class write boot, shutdown these functions are much higher efficiency.
This simulation of the project just to the previous knowledge has been linked together, once again reviewed an article, deepen the impression.
Object-oriented application of java4android Basic Learning