Java object-oriented thinking 1

Source: Internet
Author: User

1. Object-oriented is process-oriented. Both are a kind of thought.
Process oriented: Emphasis is on functional behavior. (emphasis on process, action)
Object-oriented: Encapsulates functionality into objects, emphasizing the objects with functionality. (emphasis on objects, things)
Object-oriented is process-oriented. It's easier to complicate things.
Process-oriented-object-oriented: performer-to-conductor
Encapsulates the process and function into the object.
Object-oriented three major features: encapsulation, inheritance, polymorphism. (Find objects, build objects, use objects, maintain relationships with objects.) )
The Old realm: (All things are objects!!!) )
Example: Company recruiting programmer: To improve efficiency, the boss from the process-oriented to the object-oriented (performer-to-leader)
Example: to a restaurant to eat, waiter with a la carte function is the object, we are the conductor.
2. Later write the program is to find objects to use, have objects to use, no object on the creation of an object.
People open the door: The method of noun refinement
People
Door open (door)
{
Door. open ();
}
}
Door
Open () {Operation gate axis:}
}
3. Class and object relationships: Classes are a description of things in real life. The object is such a thing, a real existence of the individual.
Describing things is describing the properties and behaviors of things.
The property corresponds to a variable in the class, and the behavior corresponds to a function (method) in the class.
Defining a class is describing a thing definition property and behavior. "Attribute" and "behavior" together become members of the class (member variables and member methods);
(Real life objects: Zhang San, John Doe.
Want to describe: these objects have common content. to the specific abstraction.
Description: These objects have the following characteristics: Name, age, gender, learning Java time, function. )
Map to Java: The description is the class defined in class. The specific object is the entity that corresponds to Java in heap memory with new.
4. The class type variable (reference type) points to the object. (Stack memory generation class type variable points to entity) (heap Memory generation entity)
5. Member variables function in the whole class, and local variables function in the function.
member variables in the heap memory exist only in memory because the object exists; local variables work in stack memory.
6. Anonymous object: New Car (). num=5; Anonymous object invocation properties are meaningless. Calling methods is meaningful. (method in method Go)
When the method of the object is called only once, the anonymous object can be used, which makes it more simplified;
If you make multiple-member calls to an object, you must give the object a name.
An anonymous object can be passed as an actual parameter.
For example: Show (New Car ()); public static void show (Car c) {c.num=3;c.color=4;} can simplify writing;
7. Encapsulation: Refers to the hidden object's properties and implementation details, only provide public access to the external way.
Benefits: Isolation of changes, ease of use, increased importance, and increased safety.
Packaging principle: will not need to provide external content is hidden;
"Hide properties and provide public access to them."
"Function is the smallest package in the code. 】.
Encapsulates a property, provides access to a get, set method, and assigns a value
Private is only accessible in private classes. Encapsulation is not private, and private is only a form of representation of encapsulation. The reason for the external access, because in the access method to add logic judgments and other statements.
A class has a property set (void) set and a Get return, then this property is private.
8. Constructor: function name and class name are identical; no return value type is defined (no void ... ); Do not write return statements.
Object one establishes the constructor that corresponds to the call.
Constructor function: You can initialize an object.
Details of the constructor: When a constructor is not defined in a class, the system defaults to a constructor that adds an empty argument to the class.
Constructors and general functions are written and run differently, and constructors run when an object is established. Initializes the object.
A generic method (set get) is an object invocation, which is a function of adding objects to an object.
Once a function is established, the constructor runs only once. The general method can be called multiple times by the function object.
9. Construct block of code, function: Initialize the object, the object is set up to run, take precedence over the constructor function execution. Constructs a block of code to initialize all objects,
constructor to initialize the corresponding object.
Different constructors have the same initialization content.
10. Local variables are assigned to the members using the This keyword. Same name
This feature: This represents the "object" of this class. This represents a reference to the object to which the function belongs.
Which object is calling this function, this represents that object.
This applies: When you define a function in this class, the function object is called internally, and this object is represented by this.
11. Call between constructors with this (), which distinguishes a variable of the same name.
The this statement can only be in the first row of the constructor.
12.static keyword. is a modifier used to decorate a member (member variable, member function) (in the method area)
Not in heap memory, objects can be shared.
Can be called by the class name. (class name. static member).
Characteristics of static statics:
1. Load as the class loads. 2. Shared by the object being used. 3. Precedence over object existence. 4. Can be called by the class name. 5. Static variables can use the GET, set method
Unique data with member variables, multiple objects that share data with static variables.
13. Instance variable and class variable (static) difference
1. Class variables are present in the method area as the classes are loaded. Instance variables exist in heap memory as the object is established.
2. The class variable has a long life cycle and disappears as the class disappears. Instance variables disappear as the object disappears.
3. The main function is static.
14. Static Use precautions:
1. Static methods can only access static members (properties, methods). Non-static methods can access both static and non-static.
2. The This.super keyword cannot be defined in a static method. (Static overrides the existence of the object, which has this in the object.)

Java object-oriented thinking 1

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.