Older Dick Silk Self-study note--java 0 basics to rookie--017

Source: Internet
Author: User

Code blocks, inheritance, this and super, methods of rewriting

1. Code block: Code that is enclosed in "{}" is called a code block.

Construct a code block: the location of a member in a class, with the code enclosed in "{}".

Static code block: Constructs a code block + static modifier.

Local code fast: local location, used to limit the life cycle of a variable.

1 classtest{2     Static {3System.out.println ("Static code block");4 }5 6     {7System.out.println ("Construct code block");8     }9 Ten      PublicTest () { OneSYSTEM.OUT.PRINTLN ("Local code block"); A     } - } -  the classfin{ -      Public Static voidMain (string[] args) { -Test t1=NewTest (); -Test t2=NewTest (); +     } - } +  A //output in turn: Static code block, construction code block, local code block, construction code block, local code block

2. Inheritance

Inheritance Overview: Extends creates new subclasses based on existing classes, and new subclasses can use the parent class functionality while defining new functionality.

Inherited format: Class subclass name extends parent class name, such as: Class Cat extends animal{}.

The advantages of inheritance: It improves the reusability and maintainability of code, makes the relationship between classes and classes, and is the precondition of polymorphism.

Inheritance disadvantage: Improved coupling (Code principle: Cohesion "ability to accomplish something alone", low coupling "class-to-Class relationship").

Inheritance characteristics: Can only single inheritance, does not support multiple inheritance, that is, a subclass can have only one parent class, can be multi-layer inheritance.

Note: Private members of the parent class cannot be inherited; The constructor of the parent class cannot be inherited, but can be accessed through the Super keyword.

The subclass method accesses the member variable: (1) in the sub-category of the local scope of the search, (2) in the subclass members of the Search, (3) in the parent class member scope to look for, (4) If none exist then the error.

Subclass methods and constructors: all constructors of subclasses have access to the parent class without a parameter constructor by default.

1 classanimal{2     Static {3System.out.println ("1");4     }5 6     {7System.out.println ("2");8     }9 Ten      PublicAnimal () { OneSystem.out.println ("3"); A     } - } -  the classCatextendsanimal{ -     Static { -System.out.println ("4"); -     } +  -     { +System.out.println ("5"); A     } at  -      PublicCat () { -System.out.println ("6"); -     } - } -  in classfin{ -      Public Static voidMain (string[] args) { toCat c=NewCat (); +     } - } the  * //Output: 1,4,2,3,5,6

3, this and super

This current class, such as: this.age; , this (parameter);.

Super Parent class, such as: super.age; , super (parameter);.

4, the method of rewriting

Override of method: Override, a method declaration that is identical to the parent class in a subclass, overrides the method of the parent class in the subclass.

Overrides and overloads (override vs. overload): Override, a method declaration that is identical to the parent class in a subclass.

Overloads, like the method names that appear in this class, are different methods of the parameter list (regardless of the return value).

Methods overridden by the application: subclasses need both the function of the parent class and the need to add their own unique features when overriding the parent class method.

1 class extends animal{2      Public void Play (string toy) {3         Super. play (toy); 4         System.out.println ("Sell moe"); 5     }6 }

Note: Private members of the parent class cannot be overridden, and when the subclass overrides the parent class method, the access permission cannot be lower than the parent class.

Older Dick Silk Self-study note--java 0 basics to rookie--017

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.