What are the features of object-oriented

Source: Internet
Author: User

Computer software systems are the ing of real-life businesses in computers, while real-life businesses are actually object associations.
Process. Object-Oriented Programming is to organize and write program code by object in the same way as the actual business, so that the computer
The system can identify and understand the program code organized and written in the way of objects, so that the business objects in real life can be mapped to the computing
Machine System.
Object-Oriented Programming Languages have four main features: encapsulation, inheritance, abstraction, and polymorphism.
1 encapsulation:
Encapsulation is the basis for ensuring the excellent controllability of software components. The purpose of encapsulation is to achieve the high cohesion and low
Coupling "to prevent the impact of changes caused by program dependency. In an object-oriented programming language, an object is the most basic unit of encapsulation.
Object-oriented encapsulation is clearer and more powerful than traditional language encapsulation. Object-oriented encapsulation is to describe the owner of an object.
The Code of sex and behavior is encapsulated in a "module", that is, a class. attributes are defined by variables, and behaviors are defined by methods.
You can directly access the attributes of the same object. Normally, remember to put the variable together with the method for accessing the variable
All member variables in a class are defined as private. Only the methods of this class can access these member variables.
The encapsulation of the current object makes it easy to find the method to be assigned to this class, and it is basically object-oriented programming. Grasp one
Principle: Put the methods and related methods for operations on the same thing in the same class, put the methods and the data they operate on in the same class
.
For example, if a person wants to draw a circle on the blackboard, there are three objects involved: person, Blackboard, circle, and which object should the circle be allocated?
Because the circle needs to use the center and radius, the center and radius are clearly the attributes of the circle. If they are defined as private members in the class
Amount, then the circle method must be allocated to the circle, it can access the circle and the radius of the two attributes, people will only call the circle
The method indicates that the circle is sent to the message, and the circle method should not be allocated to the object of the person. This is the encapsulation of the object oriented.
The object is encapsulated into a highly autonomous and relatively closed individual. The object state (attribute) is read by the object's own behavior (method) and
Change. An easy-to-understand example is that the driver stops the train, and the brake action is assigned to the driver or to the train.
However, it should be allocated to the train, because the driver itself cannot have that great effort to stop a train, only the train itself can
To complete this operation, the train must call the internal clutch, brake pad, and other components to collaborate to complete the brake operation.
The process only sent a message to the train notifying the train to brake.
Abstract:
Abstraction is to find out the similarities and commonalities of some things, and then classify these things into a class. This class only considers these things.
Will ignore those aspects unrelated to the current topic and target, and focus on the current target.
. For example, when you see an ant and an elephant, you can imagine the similarities between them, that is, abstraction. Abstraction includes behavior Extraction
Image and State abstraction. For example, define a person class as follows:
Class person {
String name;
Int age;
}
People are very complicated, and there are many aspects, but because the current system only needs to know people's names and ages
The class contains only the attributes of name and age. This is a kind of image. Using abstraction can avoid considering some attributes unrelated to the target.
Details. My understanding of abstraction is not to use a microscope to look at all aspects of a thing. This involves too much content,
To be good at dividing the boundary of the problem, you only need to consider what the current system needs.
Inheritance:
When defining and implementing a class, you can define and implement the existing class on the basis of an existing class.
The defined content is your own content. You can add new content or modify the original method to make it more suitable for special needs,
This is inheritance. Inheritance is the mechanism by which child classes automatically share parent class data and methods. This is a kind of relationship between classes and improves the availability of software.
Reusability and scalability.
Polymorphism:
Polymorphism refers to the specific type pointed to by the referenced variable defined in the program and the method called by the referenced variable during programming.
It is not determined, but it is determined during the program running, that is, the Instance Object of the class to which a referenced variable is directed.
The method implemented in the class of the issued method call must be determined during the running of the program. Because it is only available when the program is running.
Determine the specific class. In this way, you can bind the referenced variable to different class implementations without modifying the source code.
As a result, the specific method of the Reference call changes accordingly, that is, without modifying the program code, you can change
Body Code, allowing the program to select multiple running states, which is polymorphism. Polymorphism enhances software flexibility and scalability.
For example, in the following code, userdao is an interface that defines the Instance Object pointed to by the reference variable userdao
Daofactory. getdao () is returned during execution. Sometimes it points to the implementation of userjdbcdao and sometimes
This is the implementation of userhibernatedao. In this way, you can change the specific point of userdao without modifying the source code.
Class implementation, resulting in the specific code for calling the userdao. insertuser () method also changed, that is, sometimes
The insertuser method of userjdbcdao sometimes calls the insertuser method of userhibernatedao:
Userdao = daofactory. getdao ();
Userdao. insertuser (User );
Metaphor: when a person eats, do you see the left or the right hand?

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.