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.  
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值