Use C language to achieve object-oriented programming OOP

參考:https://mp.weixin.qq.com/s?__biz=MzI5NzM5MjMxNw==&mid=2247486893&idx=1&sn=58b9d99db36e79b8660baab92e0c2d94&chksm=ecb48c7fdbc3056954ce2255a1d7f5c94a31ab16abfc3186eab3b0387335e57ad072ceb9aa7e&mpshare=1&scene=24&srcid=&sharer_sharetime=1593577367260&sharer_shareid=cc5ffb1d306d67c81444a3aa7b0ae74c#rd

解釋區分一下C語言和OOP

我們經常說C語言是面向過程的,而C++是面向對象的,然而何爲面向對象,什麼又是面向過程呢?不管怎麼樣,我們最原始的目標只有一個就是實現我們所需要的功能,從這一點說它們是殊途同歸的。過程與對象只是側重點不同而已。

舉個例子吧,我現在有個計劃,要去北京,OOP語言是直接給你一個車,然後你自己設定路線去北京就好,而C語言是需要你自己製造零件,自己組裝好車,然後再自己設定路線,最後到達北京。C語言比較費勁,但是程序的效率很高。

  • The explanation distinguishes between C and OOP

  • We often say that C is process oriented and C++ is object oriented. But what is object oriented and what is process oriented?In any case, our original goal was to achieve the functionality we needed, and from that point they all lead to the same thing.Processes and objects just have a different focus.

  • For example, I have a plan to go to Beijing, OOP language just gives you a car and you set your own route to Beijing, while C language requires you to make your own parts, assemble your own car, set your own route and get to Beijing.C is more laborious, but the program is very efficient.

過程&對象?

一個對象就是由或多或少的針對這個對象的過程構成的,當然其中是少不了必要的屬性。

一個過程是針對一個或者是多個對象所進行的操作。兩者是可以互相轉換的,關鍵是哪一種方式更能適合你現在的需求,更能讓你的軟件開發錦上添花。

  • Procedures & Objects?

    An object is made up of more or less processes for that object, with the necessary attributes.

    A procedure is an operation performed on one or more objects.The two can be interchangeable, the key is which one is more suitable for your current needs and better for your software development.

C語言的特性,實現OOP

C是一門面向過程的語言,但它依舊可以實現大多數面向對象所能完成的工作。比如面向對象的三大特性:封裝、繼承、多態。我們以下圖來寫代碼舉例子。

  • C language features, OOP implementation

    C is a process-oriented language, but it still does most of the work that object orientation does.Consider the three main features of object orientation: encapsulation, inheritance, and polymorphism.Let's write the code in the following figure as an example.

封裝

由於面象向對象是將數據與方法封裝到一個類裏。使用者無需關心類是怎麼實現的。在 C_OOP 中貫徹了這一思想,C中有一種複雜的數據結構叫做struct。struct是C裏面的結構體。

如上圖假如我們要對鳥bird進行封裝,bird可能包括姓名、顏色、棲息地、重量、屬性等信息。我們就可以對它封裝如下:

  • encapsulation

    Because an image object encapsulates data and methods in a class.The consumer doesn't care how the class is implemented.This idea follows in C_OOP, where C has a complex data structure called a struct.Struct is the structure inside of C.

    If we want to encapsulate the bird, the bird may include information such as name, color, habitat, weight, and attributes.We can encapsulate it as follows:

struct Bird{
    char name[20];//姓名
    char color;    //顏色   
    char addr[30];    //棲息地
    int weight;        //體重
    int other;      //屬性
};
struct Bird p;
p.name = "bird";
p.color = 'b';  //'b' = black; 'g' = green
p.addr = 'w';  
p.weight = 175;
p.other = 1;
struct fBird{
    struct Bird p;
    char fly[20]; //飛翔
    int scream;        //鳴叫
};
struct fBird s;
s.p.name = "bird";
s.p.color = 'b';
s.p.other = 25;
s.p.weight = 65;
s.fly = "0618";
s.scream = 90;

  I hope I can help you,If you have any questions, please  comment on this blog or send me a private message. I will reply in my free time.  
 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章