<java-Object Oriented (iii) >

Source: Internet
Author: User

01. Single Case Mode:

 PackageDanli;/*** Samsung NOTE7: * Phone Model: Note7 *@authorWeguikin * * Lazy Type: * Features: Space saving * * A hungry Man: * Features: The first moment to create the object, waiting to be used*/ Public classNote7 {StaticNote7 Note;//Create a space in memory to store variable note for the custom data type of the note//non-parametric construction     PublicNote7 () {}//Create a method     Public StaticNote7 St () {if(Note = =NULL) {Note=NewNote7 (); //Note7 = new Note7 ();        }        returnNote; }}/*** A hungry man type: *@authorWeguikin **/    classNote2 {//write a class        StaticNote2 Note2 =NewNote2 ();//static loads when the class is loaded                 Public StaticNote2 GetNote2 () {returnNote2; }    }
 PackageDanli;/*** Note7: * Note This phone can only create one *@authorWeguikin **/ Public classNote7test { Public Static voidMain (string[] arge) {//Gets the method object name of the method. Method NameNote7 note1 =Note7.st (); Note7 Note2=Note7.st (); Note7 Note3=Note7.st (); Note7 note4=Note7.st ();        System.out.println (note1);        System.out.println (NOTE2);        System.out.println (Note3);                        System.out.println (NOTE4); System.out.println ("========= delimiter =================="); Note2 St=Note2.getnote2 (); Note2 NT=Note2.getnote2 (); Note2 So=Note2.getnote2 (); Note2 FE=Note2.getnote2 ();        System.out.println (ST);        SYSTEM.OUT.PRINTLN (NT);        System.out.println (SO);    SYSTEM.OUT.PRINTLN (FE); }}

02. Inheritance:

 package   extend;  /**   * Grandpa's class *   @author   Weguikin *  */ public  class   grandfather { int  money = 1200;}  
 package   extend;  /**   * Parent class: *   @author   Weguikin *  */ public  class  father extends   grandfather{ int      age;         char   gender;      // static String name = "against";  public  void       eat () {System.out.println ( I love to eat celery ... 
 package   extend;  /**   subclass *   @author   Weguikin *  */ public  class         Son extends   father {String name;  /*  public Son (int age,char gender,string Name) {//constructor}  */  @Override public  void   eat () { super      . Eat (); }}
 Packageextend;/*** Inheritance: * When we want a template, implement a variety of objects, take the code out and put it in a separate class--the stored class is called the parent class * Parent class (superclass, base class): The inherited person *             Subclass (derived Class): inheritors * Syntax: * Public class subclass name extends parent class Name {* *  * Note: * 1. One can inherit only one class directly, but other classes may be indirectly inherited * 2. If a class does not explicitly extends another class, it inherits the object class by default: A class that is a superclass of all classes * * Scope: * Subclasses can inherit all data from the parent class, including private data, but to be accessed indirectly, the constructor cannot inherit * * Benefits: * Save Code * * Method Overrides: * If the subclass and the parent class method name are identical, this method of the subclass             Called a method of rewriting * * Rewrite the place to note: * 1. The condition that constitutes the override: The return value of the method, the method name, the parameter list to be exactly the same, the range of modifiers cannot be less than the parent class * 2. Private methods and constructors cannot be overridden * 3. Static methods do not have the concept of rewriting, but can * *@authorWeguikin **/ Public classExtendtest { Public Static voidMain (string[] args) {//Creating ObjectsSon son =NewSon ();                Son.eat ();        System.out.println (Son.money);    System.out.println (Son.name); }}

<java-Object Oriented (iii) >

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.