OO IN C (4): Object-oriented thinking in C language (1)

xiaoxiao2021-03-06  64

Object-oriented thinking in C language

I often hear someone else's object-oriented programming. When I was in class, I also opened the class for object-oriented program. But unfortunately, these are based on C , even VC . More unfortunately, I have always been a C users for many years. At school, I mainly do is the drive layer on the hardware, and the underlying functional layer. After work, it is done the software development on your mobile phone, all of which are in which you are in which you are. Although I have to say that C is a very good language, but its compilation speed, code efficiency, compiled code size limit it in embedded applications. (However, the current embedded CPU is getting faster and faster, the memory capacity is bigger. I don't think there should be no problem with C . This makes me feel like an embedded compiler. Although Philip and Ti seem to have C compilation The device, but it seems that no one uses this. Is it too expensive? But no matter what, embedded applications, the general use of C language is affirmative) So whether the C language generated in the process-oriented era can use object-oriented Thought? I think it is definitely, C has joined the support of the object on the language level while providing a rich object library. And under the C language, we have to be self-reliant. I. The purpose of object-oriented thinking is to frameize, the means is abstract

I believe that many people understand what is going on: class, abstract class, inheritance, and polymorphism. But what is the cause of these concepts? Bi-side saying: You go to buy a monitor, but the brand style of the display is diverse, and what you happen in the process of buying is also unpredictable. For such things, how do we describe in the program language. Object-oriented thinking is to solve such problems. Write a program (even a project), which is difficult to use, from richness is more difficult. Object-oriented uses individual behaviors of the program to objects, and uses abstract methods to classify (abstraction), thereby simplifying intricate things into several major organic combinations (framework). In fact, there are many things around us, such as computers: Computers are composed of a motherboard, CPU plus various cards. This is a framework. Ignore different CPUs, different motherboards, different sound cards, network cards, graphics card differences, this is abstract. For example, now the current education network: is a major core node: Tsinghua, Peking University, Bei Post, etc., and then all child nodes, constitute a whole education network network. So I think that object-oriented programming is: a large engineering is a hierarchical structure, and each layer is connected to an overall (framework), and each abstract structure is independent of each other and can evolve independently (inherit, Polymorphism). The hierarchy, the structure has a unified communication method (usually a message, event mechanism).

Second, the "object-oriented" method commonly used in previous C language programming

In fact, since the birth of C language, people have thought of many ways to reflect the "object-oriented" ideas. Let's talk about the method I know. Let me say some people familiar with Dongdong, and slowly talk about it. Ha ha

1. Macro definition: Some people can't help but ask, how to pull it here, we can first look at a simple example: #define macrofunction Afunction and then call a lot of AFUNCTION in the program, but one day, you suddenly discovered You have to use BFunction, (but Afunction can't, it is very likely that you will call later), this time you can #define macrofunction bFunction to achieve this. Of course, I have to say such a way is to simple, sometime naïve, because a very funny question is if I generally change to bforction, what should I do if I don't change? Then I have to find it back. 2. Static entry function, guarantee the same name, using flag bit call subunies: This typical application is a lot, such as the network card driven has an inlet function Nilan (int functioncode, para *). The specific parameters are not clear. However, Nilan's main body is like this: Long Nilan (int functioncode, para *) {switch (functioncode) {copy sendpacket: send (....) Case receivePacket: Receive (...) ... ... ............. Bar. To ensure the same function name: NIC driver is the PNA protocol stack, so how to ensure that PNA protocol stacks and different drivers are compatible, a simple way is to use only one portfolio. Call the internal functions by changing the parameter value of the function. This approach is possible: If you want to call a new function later, it will increase the corresponding function parameter value. If we look at the network driver and PNA protocol stack as two layers, we can find that the interconnect port between layers and layers is very small (here a portal function), generally using name parsing method rather than Specific function call (using the functionCode call function, Nilan only implements the function of the name resolution) -! Interface restrictions and name analysis

Interface restriction: only a limited function between layers and layers

Name parsing: Establish a common name and function between the layer and the layer, and use the name call function.

转载请注明原文地址:https://www.9cbs.com/read-112459.html

New Post(0)