C语言的头文件和面向对象编程的可重用性?

发布于 2024-12-14 00:12:38 字数 78 浏览 4 评论 0原文

在面试中我被问到,可重用性是面向对象编程的主要优点之一,但它也可以通过在C语言中包含头文件来实现吗?那么OOP可重用性和C头文件有什么区别呢?

In interview I was ask that as re-usability is one of the main advantages of Object Oriented Programming but it can also be achieved by include header files in C language? So what is the difference in OOP re-usability and C Header files?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

故人爱我别走 2024-12-21 00:12:38

如果通过“可重用性”您只是暗示代码不需要在每个代码模块中重复,那么是的,C 中的头文件可以完成该任务,因为它允许使用外部链接定义的函数和变量的声明一个代码模块可以在另一个代码模块中使用,而用户不必重新键入所有这些声明和/或尝试将通常属于库一部分的每个函数的每个定义放入每个代码模块中。这样就避免了代码的重复。

通过在 C++ 和 Java 等语言中使用继承和多态性的面向对象编程具有类似的效果......您定义一次接口和/或基类,然后就可以通过继承“包含”该代码在另一个班级。此外,虚拟方法和多态性允许您编写采用单个基类类型作为参数的函数,但调用实际在派生类类型中定义的代码。这本质上意味着您可以在代码(即接受基类类型的函数)中调用代码(即您的派生类)。例如,作为库开发人员,您可以定义一组基类类型/接口,用户可以从这些基类派生,但仍然可以在接受参数的库中包含的相同函数中有效地使用它们的基类类型。因此,您不必被迫重复这些函数...它们仍然可以由您的“新”派生类使用。

If by "re-usability" you are simply implying that code does not need to be repeated in each code module, then yes, a header-file in C accomplishes that task because it allows the declarations for functions and variables defined with external linking in one code module to be used in another code module without the user having to re-type all those declarations and/or attempt to place every definition of every function that would normally be part of a library into each code module. Thus the duplication of code is prevented.

Object-oriented programming through the use of inheritance and polymorphism in languages like C++ and Java have a similar effect ... you define an interface and/or a base-class once, and then you are able to "include" that code via inheritance in another class. Additionally, virtual methods along with polymorphism allows you to write functions that take a single base-class type as an argument, yet call code that is actually defined in a derived class-type. This essentially means you can call new code (i.e., your derived class), in old code (i.e., the function that accepted a base-class type). For instance, as a library developer, you could define a set of base-class types/interfaces, and a user could derived from those base-classes, yet still use them effectively in the same functions that were included with the library that accept arguments of the base-class type. Thus you are not forced to reduplicate those functions ... they are still usable by your "new" derived classes.

筱果果 2024-12-21 00:12:38

基本上,无需 OOP,仅使用包含的标头,您就可以使用现有函数,而无需自己重新编写。

但是,如果您打算使用一个非常相似但略有不同的函数,您别无选择,只能自己编写。在这种情况下,您不能重用原始函数,您必须编写一个新函数。

OOP的优点:如果该函数是一个类,您可以继承它,并且只添加一些小方法,这样您就可以重用原始类的大部分方法。

Basically, without OOP and just using included headers, you can use an existing function without needing to write it again yourself.

However, if you intend to use a very similar, but slightly different function, you have no choice but to write it yourself. You can not reuse the original function in this case, you have to write a new one.

Advantage of OOP: If that function were a class instead, you can inherit from it, and only add a few small methods, so you can reuse most of the methods of the original class.

薄荷港 2024-12-21 00:12:38

这里重要的是不要混淆 OOP 的语言支持和 OOP 本身。可重用C代码的常见做法是在头文件中定义数据类型和操作该数据类型的函数,然后根据这些数据类型和函数实现函数。当您仔细观察时,您会发现这是 OOP 的实现,尽管没有适当的语言支持,因此不太稳定。但是:类型定义数据结构和对这些数据结构进行操作的函数的AC头文件是OOP的实现。

因此,代码可重用性没有区别,只是两个不同层上的视图。 OOP 是关于一种范例,C 头文件是关于 C 上下文中的实现。

It is important here not to confuse language support for OOP and OOP itself. The common practice for re-usable C code is to define data types and functions operating on the data types in an header file, and then implement functions in terms of these data types and functions. When you look at it closely, this is an implementation of OOP, even though without proper language support and thus less stable. But: A C header file that typedefs data structures and functions operating on these data structures is an implementation of OOP.

Therefore, there is no difference in code reusability, it is just a view on two different layers. OOP is about a paradigm, C headers about an implementation in the C context.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文