Dark Horse programmer _ object-oriented

Source: Internet
Author: User

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

Object-oriented

1. Definition:

Object-oriented is relative to process-oriented

Object-oriented and process-oriented thinking

2. Differences:

Object-oriented:

Is to encapsulate functions into objects, emphasizing functional objects, not focusing on the process

Process-oriented:

Focus on functional behaviors and the implementation process

3. Relationship: Object-oriented is based on process-oriented

4. Benefits of object-oriented:

It is a kind of thought that conforms to human thinking habits.

Complex things can be simplified.

Converts programmers from executors to operators.

5. Proximity principle:

If a Member is not found locally, the parent class is found if the member is not. If the parent class is not found, the parent class of the parent class is found. The process of searching, regardless of whether the value is.

6. Anonymous objects:

Usage:

Separate use: one-time use or chained call.

As a parameter: At this time, the anonymous object must have available values for the corresponding attribute.

 

Encapsulation

1. Advantages of encapsulation:

Isolate changes;

Convenience for subclass use;

Improve reusability;

Improve security;

2. Keyword PRIVATE: the modified member can be called only within the object.

3. During encapsulation, the value assignment and value methods of member variables are usually obtained as fixed names: Get/set settings.

Cause: Unknown name;

Especially in the Framework, the Set/get method of the attribute is concatenated and searched, so it must be set to get/set method, otherwise the framework cannot be used.

 

4. Constructor

Definition Format:

The function name is the same as the class name. You do not need to define the type of the returned value. No value is returned.

For example, class person {

Public Person (){}

}

Purpose: Initialize the object.

5. Special usage of this keyword in Constructor

When a constructor calls another constructor directly, an error is reported when the method name is directly used, and the method cannot be found.

In the constructor, you can use this (parameter) to call the constructor of other parameters, but it must be in the first line.

This variable can also be used when a local variable hides a member variable.

 

 

Static

1. When a property needs to be shared by all instances, the static keyword can be used.

2. Usage considerations:

Once a value is assigned to a static variable, all instances are affected.

Static methods cannot access non-static members.

This and super keywords cannot be written in static methods.

3. features:

Static members are called directly by Class Name

Loading with classes takes precedence over object existence.

Shared by all objects

4. Use {} to define the code area of the Scope

Partial code block:

Location: In Method

Function: controls the scope of a variable and affects its lifecycle.

Construct a code block:

Location: directly exists in the class

Purpose: extract the same code block from the constructor and execute it before the constructor.

Static construction code block:

Location: It exists directly in the class and is modified using static

Purpose: Initialize the member variable only once.

Execution sequence: static code block --> construct code block --> Constructor

5. Object Instantiation process:

Default Initialization

Display Initialization

Static code block (only access to static members)

Construct code blocks

Constructor

Note: The execution sequence of display initialization and static code blocks is determined by their order in the code, but it is generally displayed that the initialization is prior.

6. Differences between static and non-static:

Static modification: Shared data of Objects

Non-static modifier: Object-specific data

Use of javadoc

1. Usage: javadoc-D Doc-author-version tools. Java

2. Notes:

The file for generating the document must be a Java File

The document generation class must be public or protected

For the last generated DOC file, you only need to pay attention to the index webpage file.

 

 

Inheritance

1. Notes:

Any class directly or indirectly inherits the object class, excluding arrays;

System. Out. println (reference variable) hides a step: Call the tostring () method.

Java does not support multi-inheritance, but multiple implementations can be used.

Method rewriting adopts the proximity principle

2. Super

Super represents the ID of the parent class memory space

When a child or parent class has members of the same name, you can use the super keyword to differentiate them.

When the subclass calls the constructor of the parent class, the super statement is used.

3. Method Rewriting

Method rewriting is the relationship of sub-classes to improve scalability.

Precautions for method Rewriting:

The method body can be different (from the syntax perspective) and must be different (from the actual business perspective );

The method bodies are different. Others are the same and also called method rewriting;

Access permission: subclass greater than or equal to the parent class;

The method name and parameter list must be the same;

Return Value Type:

When the return value type of the parent class is basic data type (including void), the return value type of the subclass method must be the same

When the return value type of the parent class is the reference type, the return value type of the subclass method (this is the reference type) must be the Child class of the parent class;

Method Overloading is not equal to method rewriting;

Static methods can only be rewritten. Static methods can only be rewritten;

 

Constructor relationships after inheritance

1. constructor cannot be inherited, but can only be called (so it cannot be overwritten)

2. When a subclass creates an object, it first calls the non-parametric constructor of the parent class, and then calls the non-parametric constructor of the subclass.

Although the constructor of the parent class is called, the Instance Object of the parent class is not created, but there is a memory space in the subclass.

3. the constructor of the parent class only initializes the variables.

4. Super () exists in the constructor. This statement is executed first in the first line. A super statement with parameters does not exist by default. After the super statement is executed, consider the creation process of the Instance Object.

5. If the parent class does not have a constructor without parameters, you must manually give a super statement with parameters for calling. Or use this statement to call other constructors.

6. This and super cannot exist at the same time.

 

 

Final keywords

The class modified by final cannot be inherited.

Final modified variables cannot be modified

The final modification method cannot be overwritten.

Note:

The final variable must have a value. You can assign a value before the constructor ends. You can assign values in the constructor or code block.

For the referenced data type (except the string type), the internal attribute can be changed after final is modified. Because the address of the referenced object is not changed.

 

 

 

Polymorphism

1. Prerequisites for Polymorphism:

Inheritance or implementation

Method rewriting, otherwise it does not make sense

The parent class or interface points to the referenced instance object.

2. polymorphism features:

Member variables:

During compilation, check whether the variable is on the left;

When running, check the value of the variable on the left.

Non-static member methods:

During compilation, check whether this method is available on the left;

During running, check whether the method is rewritten on the right. If yes, call the method on the right. If no, call the method on the left.

Static member method: Compile and run all the statements on the left, that is, the parent class.

3. Benefits of polymorphism:

Improves program scalability and maintainability

 

 

Abstract class

Abstract classes cannot be instantiated;

After the subclass inherits the abstract class, you must override the abstract method. Otherwise, the instance object cannot be created;

Abstract methods exist in abstract classes.

 

 

 

Interface

1. interface features:

A. Methods in interfaces are abstract.

B. modifier of interface members can be omitted.

Features of member variables:

Modified by public static final

Value required

The name must be capitalized.

Can be called by Class Name

Features of member methods:

Modified by public abstract

Method bodies are not allowed

When a subclass overrides an interface method, it must be modified with public.

C. The interface has no constructor. Because of the constant number of member variables, Initialization is not required.

D. interfaces can only inherit interfaces.

E. The implementation class must override all methods related to its interface.

2. Interface considerations:

Multiple Interfaces can be inherited;

The interface has no constructor.

 

3. Differences between interfaces and abstract classes:

Commonalities: all concepts are constantly extracted;

Differences:

A. abstract classes reflect the inheritance relationship. A class can only be inherited by a single user.

Interface represents the implementation relationship, multiple implementations of one class

B. the abstract class is the relation of "is ".

Interface is the relationship of "like"

C. A non-abstract class can be defined in an abstract class for the subclass to call directly.

All interfaces are abstract and have fixed modifiers.

 

The brief description is as follows:

Abstract classes are the common content of the inheritance system;

Interfaces are extended content of the inheritance system.

 

Internal class

1. Internal classes cannot directly create objects

2. Access features:

Internal classes can directly access internal class members, including private members.

To access the internal class from the outside, you must create an instance of the internal class.

3. Two definitions of the internal class:

Member location:

A. Access format:

Outer. Inner x = new outer (). New inner ();

Equivalent to data type: innner type in outer

To create an internal class instance, you must create an external class instance, that is, call the outer and inner constructor methods respectively (without considering static conditions ).

B. Internal classes are private to ensure data security;

The internal class is modified by static to facilitate calling.

Static modified ACCESS format: outer. Inner x = new outer. Inner ();

Call the static internal class method: outer. Inner. Method ();

Local Location:

You cannot directly create instance objects of internal classes in other classes;

You can also directly access external class members;

You cannot use outer. Inner for access;

The ACCESS format is as follows: Call the method after the object is created in the method, and access the local variables in the local directory, but the final modification is required.

 

 

Anonymous internal class

1. Local internal class

2. Prerequisites:

An internal class can implement or inherit an interface or an external class.

3. format:

New Class Name or Interface Name (){

Override methods in the class or interface, or define the content by yourself.

};

4. Benefits of using anonymous internal classes:

High efficiency of internal classes;

Enhance readability.

Note: When an interface or abstract class has multiple methods, it is very troublesome to use an anonymous internal class.

 

 

 

 

Package

1. Access the four modifiers in a class:

Public protect default private

Access this type: √

Class access in the same package: √ ×

Subclasses in different packages: √ ××

Unrelated classes in other packages: √ ×××

 

2. Considerations for cross-package access:

During cross-package access, the accessed class needs to be modified by the public.

You can export packages to solve the access problems in different packages.

When multiple packages have classes with the same class name, only this class in one package can be imported. In addition, classes with the same name in the package can only use the limited name (package name. Class Name ).

3. Package naming rules:

Use package to add the specified package name to the first line. Such as package CN. itcaseheima;

All are in lower case. For multi-level packages, separate them with "." And reverse write the domain name. For example, the Black Horse package itcaseheima.cn should be written as CN. itcaseheima.

4. Compile classes in the package:

Use javac-D. Class Name. Java to generate the class file directly into the package.

Running classes in the package:

Permission restriction name: Java package name. Class Name.

 

Dark Horse programmer _ object-oriented

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.