What is the difference between object-oriented programming and process-oriented programming?
C language is process-oriented programming. It is the most important feature of a function. It calls subfunctions through the main function. The program running sequence is determined by the programmer. It is the first programming language I have learned.
C ++ Is Object-Oriented Programming and a class is its main feature. During program execution, the main function first enters and defines some classes. As needed, it executes the member functions of the class, the concept of a process is deprecated (in fact, there are still some statements of the main function). Classes are objects, so we call them object-oriented programming. Differences: 1. Programming Model
All computers are composed of two elements: code and data. specifically, some programs are written around "what is happening", while others are written around "who is being affected.
The first programming method is called "process-oriented model". A program written based on this model features a series of linear steps (CODE) and can be understood as code acting on data. such as C and other procedural languages.
The second programming method is called "object-oriented model". programs written based on this model organize programs around program data (objects) and strictly defined interfaces for this object, it features data control code access. by transferring control to data, the object-oriented model has the following benefits in terms of organization: abstraction, encapsulation, inheritance, and polymorphism.
2. Abstraction
The basic element of object-oriented programming is abstraction. Programmers manage complexity through abstraction.
An effective way to manage abstraction is to use hierarchical classification features, which allows users to break down a complex system based on their physical meanings and divide it into more manageable blocks. for example, a computer system is an independent object. inside the computer system is composed of several subsystems: Display, keyboard, hard drive, DVD-ROM, floppy disk, audio and so on, each of these subsystems is composed of specialized components. the key is to use hierarchical abstraction to manage the complexity of computer systems (or any other complex system.
The essence of object-oriented programming: these abstract objects can be seen as specific entities that respond to messages that tell us what to do.
/* (My understanding)
* A computer is an entity. I want to enter characters and display them on the monitor.
* Computer (object). Input (Keyboard attribute). Display (Display Method)
* Use layering to reference and operate, instead of having to worry about how the computer works internally.
* If a computer object exists, it can respond to my operations, And I press the keyboard,
* The computer object sends the message to the screen, which is displayed on the screen.
*/
A computer object contains all its attributes and operations. This is one of the three principles of object-oriented programming: encapsulation.
3. Encapsulation
Encapsulation is a mechanism that binds data operated by code and code to prevent external interference and misuse. encapsulation can be understood as a protected package to prevent arbitrary access to code and data by other Code defined by the package. access to the internal code and data of the package is controlled through a clearly defined interface. the advantage of code encapsulation is that everyone knows how to access the code and can directly use it without considering implementation details, without worrying about unexpected side effects.
In JAVA, the most basic encapsulation unit is a class. A class defines the behavior (data and code) that will be shared by a group of objects ). each object of a class contains the structure and behavior defined by it. These objects are like a model. so the object is also called a class instance.
When defining a class, you must specify the code and data that constitute the class. in particular, the object defined by the class is called a member variable or an instance variable. the code used to operate data is called the member method. the method defines how to use member variables, which means that the behavior and interface of the class must be defined by the method that operates the instance data.
Because classes are used to encapsulate complexity, there is a mechanism to hide the complexity of the classes. therefore, JAVA provides private and public access modes. The public interfaces of classes represent everything that external users should know or can know. private method data can only be accessed through the member code of this class. this ensures that no unexpected events will occur.
4. Inheritance
Inheritance refers to the process in which an object obtains attributes from another object. it is the second principle of object-oriented programming. It supports the concept of hierarchical classification. for example, a persian cat is a type of CAT, a cat is a type of mammal, and a mammal is an animal. if the hierarchical concept is not used, each object must clearly define all its features. in hierarchical classification, an object only needs to be defined in its class as a unique attribute, and then inherits its general attributes from its parent class. therefore, the inheritance mechanism makes an object a specific instance of a common class. A subclass of deep inheritance inherits all the attributes of each ancestor in the class hierarchy.
Inheritance and encapsulation can interact with each other. if a given class encapsulates certain attributes, any subclass of it will contain the same attributes and add all attributes of each subclass. this is an important concept of linear rather than geometric growth in the complexity of object-oriented programs. the new subclass inherits all attributes of all its ancestors. sub-classes and other code in the system do not have unexpected interactions.
5. Polymorphism
Polymorphism means that a method can only have one name, but there are many forms, that is, the program can define multiple methods with the same name, described by "one interface, multiple methods. you can reference the parameters and types of methods.
6. encapsulation, inheritance, and multi-state combination
In an environment composed of encapsulation, inheritance, and polymorphism, programmers can compile programs that are more robust and scalable than process-oriented models. the carefully designed class hierarchy is the basis for code reuse. encapsulation allows programmers to port programs without having to modify the code of the public interface. polymorphism allows programmers to develop concise, easy to understand, and easy to modify code. example: Automobile
From the inheritance point of view, drivers rely on inheritance to drive different types of (subclass) cars. Whether this car is a car or a truck, a Mercedes-Benz card or a Fiat card, the driver can find the steering wheel, handbrake, shift changer. after a period of driving, you can know the difference between the manual and automatic shift, because they actually know the common category of the two: transmission device.
From the encapsulation perspective, drivers always see encapsulated features. the brakes hide a lot of complexity, and the appearance is so simple that you can operate it with your feet. the implementation of the engine, handbrake, and tire size has no impact on the definition of the brake class.
From the perspective of polymorphism, there is a distinction between positive lock and reverse lock in the brake system. The driver uses only the foot to stop the brakes. The same interface can be used to control several different implementations (positive lock or reverse lock ).
In this way, independent components are converted into vehicles. similarly, by using object-oriented design principles, programmers can combine components of a complex program to form a consistent, robust, and maintainable program.