Dark Horse programmer-encapsulation, constructor, this keyword, static sorting

Source: Internet
Author: User

Http://edu.csdn.net/heima Android Training
Http://edu.csdn.net/heima; Java training is coming to communicate with you!

1. encapsulation.

1. encapsulation Overview

1. encapsulation: hiding the attributes and implementation details of an object and providing only public access.

2. Benefits:

1. Isolate changes.

2. Easy to use.

3. improve reusability.

4. improve security.

3. encapsulation principles:


Hide content that does not need to be provided externally.


Hide all attributes and provide methods to access them. Set get

2. Private attributes: Use of the Set () Get () method.

Requirement: Describe the person object, attribute age, and the age of the person.

Benefits: You can control attributes.

PRIVATE: Private is a keyword, permission modifier, used to modify members (member variables, member functions)

Note: An Attribute corresponds to two methods set get.

Functions are just a situation of encapsulation. Function, class.

Ii. Constructor

1. Constructor

1. features:

1. The function name is the same as the class name.

2. the return value type does not need to be defined.

3. There is no specific return value type.

If there is no custom constructor, a default null parameter constructor will appear in the class.

2. Purpose: Initialize the object.

2. constructor and general function are different.

1. The writing format is different.

2. different operations.


Constructor: it is called once an object is created and executed only once.


General functions: an object can be called multiple times only when it is needed.


Note: constructor has a return statement to end the initialization of constructor.


Description of multiple constructors. Constructor initialization process: Draw a memory diagram.

3. small details.

4. When will constructors be used?


When describing a thing, it has some attributes and behaviors as soon as it exists, so these contents are defined in

Constructor.

3. This keyword.

1. This:The application that represents the current object. The object that calls the function where this is located represents the object.

2. When will this be used?

When defining a function, use this to indicate the objects that call the function.

This is manifested in the form of 1: when the member variables and local variables have the same name, to distinguish them, use the this keyword.

This is manifested in the form of 2: This keyword can be used for calls between constructors, followed by parentheses.


Note: The this statement that calls the constructor in this class must be defined in the first line of the constructor. Because initialization must be completed first.

3. code block.

Partial code block: controls the variable declaration period.

Construct a code block: You can initialize all objects.

Constructor: Initialize the corresponding object.

Static code block: Initialize the class.


Note: The code block construction method is the same as that of a local code block.

4. Static keywords.

1. Static keywords:

1. Modified variables are shared by all objects.

2. Static Loads with classes and disappears with the disappearance of classes.

3. takes precedence over object existence.

4. Static members can be called directly by class names.

2. Memory: static members are stored in the Method Area and shared area.

3. Static usage considerations.

1. Static methods can only call static members, but cannot call non-static members. Because the static file exists first.


Non-static methods can call non-static members or drop static members.

2. This or super keywords are not allowed in static methods.


Because the static state exists first, and the object appears later.

3. The main function is static.


Call status of the main function.

4. Static drawbacks.

1. Static methods have access limitations and can only access static methods.

2. the lifecycle is too long.

5. What are the differences between static and non-static member variables?

1. Name.


Non-static member variables are also called instance variables.


Static member variables are also called class variables.

2. Claim period


Non-static member variables. The object disappears as it is created.


Static member variables exist as the class is loaded and disappear as the class disappears.

3. Memory


Non-static variables exist in the object heap memory.


Static variables exist in the method area.

4. Call


Non-static variables can only be called by objects.


Static variables can be called by class names or objects.

7. When should I use static data? (Important)

1. member variable: if the value of the member variable is the same for every object, it is modified using static.

2. member function: If the function does not access the special properties of the object, it can be modified by static.


That is to say, when this is not in a function, you can use static.

Note: If the methods defined in a class are static, this class is a tool class and does not need to be created.


To prevent object creation: You can privatize constructors.

8. Static code block.


Features:

1. It is executed only once as the class is loaded.

2. It takes precedence over the execution of the main function.


Purpose: Initialize the class.


When constructors in a class are privatized, classes can be initialized by static code blocks.

Http://edu.csdn.net/heima Android Training
Http://edu.csdn.net/heima; Java training is coming to communicate with you! See http://edu.csdn.net/heima for details

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.