Object-oriented application of java4android Basic Learning

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.