Dark Horse Programmer _java Object-oriented (i)

Source: Internet
Author: User
Tags least privilege

-------Android Training, Java training, look forward to communicating with you! ----------

Object-oriented is a process-oriented programming idea, which is to describe the attributes and functions of things, and abstract the process of extracting encapsulation. Object-oriented is the local process-oriented, the previous study of the function extraction written into the method is actually the extraction of the process, and object-oriented, is to encapsulate the method into an object, so that it has this function, when the need to use this function directly call the object with this function, without the specific guidance of the internal implementation details.

Object-oriented phase-oriented process simplifies writing, improves the reusability of code, and makes the implementation logic of the function simple.

Object oriented
Process-oriented and object-oriented differences
The function behavior of the process-oriented emphasis
Object-oriented is to encapsulate functionality into objects, emphasizing the objects with functionality.
Object-oriented is the process of the local area, the previous study of the function extraction written into the method is actually the extraction of the process, and object-oriented, is to encapsulate the method into an object, so that it has this function.

The determination of the number of objects; the method of noun refinement
People open the Door
Class Person {
Door open (door)
Door. Open
}
Class Gate {
Open (shaft driven door)
}

Three major characteristics of object-oriented; encapsulation, inheritance, polymorphism
The process of later development is basically looking for objects, creating objects, using objects, and maintaining relationships between objects.
Class and object relationships,
Class is the abstract description of things in real life, objects are examples of these things,
Mapped to Java, the description is class defined by class. The specific object is to create entities in the heap memory with new in the corresponding java. A class is like a car's design drawing, the car that is produced is the object, and the description of the thing is describing the property and behavior of the thing. A property corresponds to a variable in a class, and the behavior corresponds to a function method in a class, so defining a class is defining properties and behaviors, members of those classes, member variables, and member methods.
Object Creation Format
Class Name Object name =new class name (), class instantiation, at this time in the memory heap to produce an object instance, C is a class type variable, there is a stack in memory point to the object,
Use format for object members
Object. Object member

From here we can see that there is only one main function as the entry reason, as long as the data of other objects are encapsulated in the form of a class, only the main function to invoke the line.

The difference between a member variable and a local variable
Member variables act on the entire class, and local variable variables act on the function or in the statement.
The location of the two in memory is also different, the member variable exists in the heap memory because the object exists, and the local variable exists in the stack memory.

Anonymous objects
An anonymous object is simply instantiating an object but does not name it to create a class type variable, there is no reference to the entity in the heap memory in the stack memory, and each time the anonymous object statement executes because it is no longer used, it is not timed for memory reclamation.
One way to use an anonymous object; When an object's method is called only once, it can be done with an anonymous object, which makes it easier. When a call is made to multiple members of an object, the object must be given a name.
How anonymous objects are used nephew: You can pass an anonymous object as an actual parameter. Simplify and optimize memory at the same time.

Actual participation in the parameter, the function is called in the stack memory, and when a function ends, the memory is recycled.
public static void Main (String[]args) {
Show (new Car ());
}
Class car{

}
public static void Show (Car c) {

}

Encapsulation: Refers to the properties and implementation details of hidden objects, providing public access only to the outside.
Benefit: Isolate the change. Easy to use. Improve reusability and improve security.
Encapsulation principle: Hide content that does not need to be provided externally. Hides properties, providing public methods for accessing them.
A function is the smallest functional unit in the code, which is the smallest package.

Private: Proprietary, permission modifier, which is used to decorate a member (member variable, member method) in a class. Private can only be accessed in this class. When access is required, the access method needs to be set, and a private member variable usually corresponds to two access methods, set or get.

Private is the least privilege in encapsulating hidden.

constructor function
When an object is analyzed, it has some properties or behavior when it exists, and it needs to be defined in the constructor.
When an object is established, the corresponding constructor is called, and when a constructor is not defined in a class, the system defaults to the constructor of an empty argument for the initialization of the object, which is no longer added when the constructor is customized in the class. When a custom constructor does not have an empty argument constructor, it is wrong and must have null parameter constructors.
The constructor differs from the general function in that the constructor is called automatically when the object is established, whereas the general method is executed only when the object is called, and the constructor runs only once, and the general function can be called multiple times.
Characteristics
1, the function name is the same as the class name
2, no return value type defined
3, can not write return statement
Role
Can be used to initialize an object.

Building code Blocks
function is similar to a constructor, but the execution order takes precedence over the constructor, and the construction code block is uniformly initialized for all objects, and the constructor initializes only the object that corresponds to the parameter. Defines the commonality initialization.

this keyword
This keyword is used to differentiate between local variables and member variables with the same name
Person (String name) {
This.name=name;
}
This refers to a reference to the current object of this class. That is, the current object name, class type variable.
The invocation of members in a class is done through objects, so it is easier to understand the order in which the code logic occurs based on the memory graph. In fact, when you do not distinguish between different times, if the member calls when the money is not written this is generally omitted.
Who called the method, this represents who.
In a constructor call, a generic function cannot call a constructor.
The This statement calls the constructor only where the first statement is placed.
Person () {}
Person (String name) {
This ();//The call between constructors can only be used with the This keyword.
This.name=name;
}
Person (String Name,int age) {
This (name);//The previous constructor is called to implement the assignment of name to simplify the code.
this.age=age;//here is just an ordinary usage of the current object's class type variable.
}

Static keyword, which is used to decorate members of a class, uses static decorated member memory not in heap memory, but rather extracts it into other areas of memory-the method area (the shared area, the data area, which holds the shared members in the class. ), which is shared by the object to optimize memory.
In addition, the member modified by the static can be called by the object or directly by the class name, and the format class name. Static members
So the member that static modifies should be the commonality of all objects.

Characteristics of static Statics
1, loaded with the load of the class
2, precedence over object existence
3, shared by all objects
4, can be called directly by the class name

Static usage Considerations
1 static methods can only access static members, non-static access to static members can also access non-static
The This,super keyword cannot be defined in a 2 static method because static overrides the existence of the object. So the static method does not appear in this,
3 The main function is static. The invocation of the main function is generally called by a class, or by an object call, rather than directly. A member access of the same class as the main function can only access static.
Lee: Storage of shared data for objects in separate spaces saves space, and it is not necessary to store one copy of each object.
Cons: The life cycle is too long and access is limited.

Main function of Main
The main function is a special function, which can be called by the JVM as the entry of the program.
Public, highest access rights
Static, which is loaded in memory as the class is loaded, so it can be called directly
Void, the main function does not have a specific return value
Main, not a keyword, but a special word that can be identified by the JVM
parameter, which is an array of string elements
The format of the main function is fixed: JVM recognition
public static void Main (String[]args) {
}
Args is a shorthand for arguments, and this variable name can be arbitrary. It's just that the habits are written like this. When the JVM invokes the main function, the arguments are passed by default to new String[0]; A string array of length 0. This parameter can be changed.

When to use static
Define static variables, when shared data exists for all objects
Define static functions, which can be defined when non-static data (that is, object-specific data) is not accessed inside the method. That is, when you do not need to use an object

static applications
Each application has a common feature, which can be extracted and packaged separately for reuse.

Static code block
feature, as the class is loaded and executes only once, the difference from constructing a block of code is also the difference between the execution precedence, which is used for initialization of the class.

Dark Horse Programmer _java Object-oriented (i)

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.