Javase basic Review 2: object-oriented knowledge points

Source: Internet
Author: User

------- Android training, Java training, and hope to communicate with you! ----------

1. Function overload

In the same class, functions with the same name but different parameter lists (number of parameters or parameter type) constitute heavy loads.
The type of the return value of the function is irrelevant to the overload.
That is, if the parameter list is the same and only the return value type is different, it is not allowed to appear in the same class, and an error occurs during compilation.
Overloading of variable-length parameters

2. Memory Division:

Heap heap memory: stores new things, objects, arrays, and initialized values.
Stack memory: stores local variables, references, and ends with the scope and lifecycle. The memory is automatically released.
Datasegment: stores string constants and static variables.
Codesegment: storage code
 
Object-oriented is an idea.

Three object-oriented features: encapsulation, inheritance, and Polymorphism

3. encapsulation: hides the attributes and implementation details of an object and only provides public access.

Benefits: Isolation of changes; ease of use; Improved reusability; Improved security
Principle: Hide the content that does not need to be provided externally. Hide the attributes and provide public methods for external access.

Public protected default private
Same class OK
Same package OK
Subclass OK
Different packages OK

4. Considerations for Object-Oriented Programming:

1. What classes and objects are there?
2. attributes and methods of classes and objects
3. Relationships between classes and objects (Association, inheritance, aggregation, polymorphism, and implementation)

Clustering: Has A depends on the closeness of things.
Combination: hand and heart
Aggregation: Teams and players
Inheritance relationship: parent-child relationship

5. class:

Data and a set of data operations are descriptions of things;
Includes attributes (also called member variables) and member methods.
Object: class instance, new stuff

5.1. member variables and local variables:

1. The member variable scope is in the entire class; the partial variable scope is in the function body or statement.
2. member variables exist in heap memory; local variables exist in stack memory.
3. member variables are created with the creation of objects and disappear with the disappearance of objects. Local variables exist only within the scope, and the end of the scope disappears.
4. The member variables have initialization values by default. Local variables need to be manually initialized.

5.2 constructors:

Creates a function called to initialize an object.
Like the class name, there cannot be a return value type or a return statement.

1. When an object is created, the corresponding constructor is called to initialize the object.
2. If no constructor is defined, there is a default constructor with null parameters in the system. After a custom constructor is defined, the default constructor disappears;
3. constructor and general functions are different in writing and running;
A constructor is called only once when an object is created. Generally, a function can be called multiple times and is called only when necessary;
A constructor can call a general function internally.
4. constructor can have multiple constructor types to initialize objects in a targeted manner and reflect them in an overloaded manner (different parameter lists );
5. If void is added before the constructor, it becomes a general function.
6. the constructor can be privatized. However, if privatized, the constructor cannot be accessed when an object is created.

The default no-argument constructor. If the class is modified by public, the constructor is also common.

Javadoc usage.

5.3 code blocks in Java

Construct a code block: (a code block without a name ){...} The constructed code block is enclosed in braces.
Initialize the object.
Objects are run when they are created and take precedence over constructors. Define common initialization content.

The constructor code block initializes all objects and the constructor initializes the corresponding objects.

Static code block. (Static modified unsung code block)
It is executed as the class is loaded. And only once. If the class has a primary function, it takes precedence over the execution of the primary function.
Static code blocks can only access static members.
Purpose: Initialize the class.

First, execute the static code block and initialize it explicitly. When using the constructor, execute the constructor to construct the code block and execute the constructor.
The static code block initializes the class, the Construction Code block initializes the object, and the constructor initializes the specified object.

Analyze the execution process of person P = new person ();
1. Load the person. Class file and enter codesegment
2. Load Static code blocks
3. Open up space in heap memory and allocate memory addresses
4. Create a special attribute of the object in the heap memory and perform default initialization.
5. Display and initialize attributes (default attribute initialization value assignment)
6. Construct code block initialization for objects
7. initialize the corresponding constructor for the object
8. Assign the memory address to the variables in the stack memory.

5.4 anonymous objects:

New is an anonymous object.
Usage of anonymous objects:
1. When an anonymous object method is called only once, an anonymous object can be used.
2. Anonymous objects can be passed as actual parameters.

All simplification must have limitations:
For example, the three-object operator simplifies the if else statement, but the three-object operation must have an operation value and only one statement.
The method of an anonymous object can only be called once.

5.5. This Keyword:

Indicates the reference of the object to which the function belongs. The super keyword is a reference to the parent class.
This application:
Distinguish between local variables and member variables with the same name.
When defining a function in a class, this is used to represent the object that calls the function.
This class object is used internally in this class function, which is represented by this.

The constructor has repeated content or functions. It cannot be called like a common function. You need to use this, which indicates the object.
This statement is used to call each other between constructors.
This statement must be the first statement of the constructor, because the initialization action must be executed first, and the initialization action must also be performed.

5.6 static keywords:

What is the difference between a member variable and a static variable?
1. The lifecycles of two variables are different.
Member variables exist with the creation of objects and are released with the collection of objects.
Static variables exist with the loading of classes and disappear with the disappearance of classes.
2. The call method is different.
Member variables can only be called by objects.
Static variables can be called by objects and class names.
3. aliases are different.
Member variables are also called instance variables.
Static variables are called class variables.
4. Data storage locations are different.
The member variable data is stored in the heap memory object, so it is also called the special data of the object.
Static variable data is stored in the static area of the Method Area (shared data area), so it is also called the shared data of objects.

Static usage considerations:
1. Static methods can only access static members. (Non-static can access both static and non-static)
2. This or super keywords cannot be used in static methods. Static takes precedence over objects.
3. The main function is static.

When is static data used?
1. Static variables.
When the values of all member variables in the analysis object are the same.
This member can be modified statically.
As long as the data is different in the object, it is the unique data of the object, which must be stored in the object and non-static.
If the data is the same, the object does not need to be modified, but only needs to be used. It does not need to be stored in the object and is defined as static.
 
2. Static functions.
If the function is statically modified, you can refer to whether the function has accessed the special data in the object.
To put it simply, from the source code, whether the function needs to access non-static member variables is non-static if needed.
If you do not need this function, you can define it as static. Of course, it can also be defined as non-static,
But it is not static and needs to be called by the object.

5.7 tools:

Static methods are used without special data.
Extracts the common functions of an application to enhance reusability. The methods in the class are static methods and encapsulated into a tool class.
And Private the non-argument constructor. Because it makes no sense to create an object for this class.

Tool-Type Static import impport static

6. Inheritance:

Classes in Java can only be single-inherited, but can be implemented in multiple ways. Multiple inheritance is supported.
Inheritance System, view the parent class function, and create a subclass instance.
Java interfaces can be inherited.
The method to implement the interface must be public, because the interface method is public by default.

6.1 overwrite ):

When the sub-parent class has identical member functions, the sub-class functions are run.

Considerations:
1. When the subclass method overwrites the parent class method, the permission of the subclass must be greater than or equal to the permission of the parent class.
2. static data can only be overwritten or overwritten.
3. The sub-parent class methods should be the same

Reload indicates whether the parameter list in the same class is the same.
Override means that the sub-parent class methods must be identical.

6.2. Coverage principle: the two are the same as the two smaller ones.

The two are the same: function name and parameter list are the same
Two smaller values: the return value type of the subclass and the exception thrown by the subclass are smaller than those of the parent class.
Major: the sub-class method has higher access permissions than the parent class method.

When do I use overwriting?
When extending a class, the subclass must retain the function declaration of the parent class,
However, to define the special content of this function in the subclass, The overwrite operation is used.

1. When a subclass overwrites the parent class method, if the parent class method throws an exception,
The subclass method can only throw an exception of the parent class or a subclass of the exception.
2. If the parent class throws multiple exceptions, the Child class can only throw a subset of the parent class exceptions.
Child classes override child classes that can only throw exceptions of the parent class.
Note: If the method of the parent class does not throw an exception, you must try to overwrite the Child class.

The super keyword can reference the members in the parent class and access hidden members of the parent class in the subclass.

6.3 usage of constructor in inheritance:

The constructor In the subclass calls the non-argument constructor of the parent class by default,
The parent class constructor must be in the first line, super (); if this () exists, there cannot be super ()

6.4. Final keywords:

Variables modified by final cannot be modified and are capitalized as constants. Multiple words are separated;
Classes modified by final cannot be inherited, and Methods Modified by final cannot be overwritten.

Abstract keywords: abstract classes and abstract methods.

7. Polymorphism:

There are multiple forms of transactions. parent class references point to subclass objects (parent class and subclass exist overwrite or implement ).
To improve scalability, you can only use the parent class reference to access members in the parent class.
After the parent class references the Child class object, the Child class is promoted and transformed upwards;
If you want to use a subclass-specific member, you need to perform a downward transformation to forcibly convert the type of the parent class reference. However, the parent class object cannot be converted into a subclass.

7.1. Characteristics of members during polymorphism: static binding dynamic binding

 

Base base = new sub ();

Static binding: for static members, bind to the class

Dynamic binding: non-static members. This reference is not fixed.

1. When base calls the member variable.
During Compilation: Check whether the base class contains the called member variables. If yes, the compilation is successful. If no, the compilation fails.
Runtime: Check whether the base class contains the called member variables and call the member variables in the base class.
To put it simply, the value of the compiled and running member variables depends on the left side of the equal sign.
 
2. member functions (non-static ).
During Compilation: Check whether the base class has called functions. Yes, compilation passed, no, compilation failed.
Runtime: run the function in the subclass.
To put it simply: Compile to the left and run to the right.
Because the member functions have the overwriting feature.
 
3. Static functions.
Compile time: Check whether there are static methods called in the base class.
Runtime: Run static methods in base.
To put it simply, the compiling and running operations are on the left.

In fact, objects are not required for static methods. You should directly use the class name for static binding.

------- Android training, Java training, and hope to communicate with you! ----------

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.