C语言是面向对象的吗?
我和一位同事谈论 C 和 C++,他声称 C 是面向对象的,但我声称不是。我知道你可以用 C 来做类似面向对象的事情,但 C++ 是一种真正的面向对象语言。
你有什么想法?
此外,它还引发了关于谁来决定面向对象意味着什么的讨论,而且很难确切地说出面向对象的正式含义。您对此有何看法?
I was talking with a co-worker about C and C++ and he claimed that C is object-oriented, but I claimed that it was not. I know that you can do object-oriented-like things in C, but C++ is a true object-oriented language.
What are your thoughts?
Also, it triggered discussion on who decides what it means to be object-oriented and that it's tough to say what object-oriented really officially means. What are you thoughts on this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(16)
如果通过“C 是面向对象的吗?”你的意思是“C 是专门为支持面向对象编程而设计的吗?”那么,不,C 显然不是面向对象的。
If by "is C object oriented?" you mean "is C designed with facilities specifically to support object oriented programming?" then, no, C is clearly not object oriented.
您可以使用或多或少的任何语言以面向对象的方式进行编程。 (我认为运行时多态性——即虚拟方法——需要一种支持函数指针的语言。)
这里有几个例子:
You can program in an object-orientated style in more or less any language. (I think runtime polymorphism -- i.e. virtual methods -- requires a language that supports function pointers.)
Here are a couple of examples:
C 不是面向对象的。这就是 ++ 背后的全部目的。
至于面向对象的定义:检查wikipedia 。
就我个人而言,如果它支持继承、封装和多态性,那么就可以了。这里的另一个关键是拥有像 class 和 object 这样的好关键字往往会有所帮助...
真正的面向对象语言的示例(不是决定性的)是:Smalltalk、Java、c#、Python、Ruby、C++..
此外,还可以扩展提供 OO 功能,如 PHP、Perl、VB(不是 .Net)...
C isn't object oriented. That was the entire purpose behind the ++
As far as a definition of what it takes to be object oriented: check wikipedia.
Personally, if it supports inheritance, encapsulation, and polymorphism then your good to go. Another key here is having nice keywords like class and object tend to help...
Examples of real object oriented languages (not conclusive) are: Smalltalk, Java, c#, Python, Ruby, C++..
Also, it's possible to have extensions to provide OO features like PHP, Perl, VB (not .Net), ...
但不,C 不是一种“面向对象”语言。它没有类、对象、多态、继承的概念。
But no, C is not an 'object-oriented' language. It has no concept of classes, objects, polymorphism, inheritance.
答案可以是或否,具体取决于:
如果您问“C 是一种面向对象语言吗?”,答案是“否”,因为它不有面向对象的构造函数、关键字、语义等...
如果你想要“我可以用C实现OOP吗?”,答案是肯定的,因为OOP不仅是一种语言的要求,也是一种语言的要求。在接触某种语言或另一种语言之前,“思维”的方式,一种编程的方法。然而,在 C(或任何其他非本机设计为 OOP 的语言)中实现 OOP 肯定是“强制的”,并且比任何其他 OOP 语言更难以管理,因此也应该有一些限制。
Answer can be yes or no, depending on:
if you ask "is C an object oriented language?", the answer is "no" because it do not have object oriented constructors, keywords, semantic etc...
if you intend "can I realize OOP in C?", the answer is yes, because OOP is not only a requirement of a language, but also a way of "thinking", an approach to programming, before to touch some language or another. However the implementation of OOP in C (or any other language not natively designed to be OOP) will be surely "forced" and much hard to manage then any other OOP language, so also some limitation shall be expected.
根据“面向对象”和“语言”的任何定义,C 都不是面向对象语言。
使用 C 作为向其客户端提供 OO API 的组件的实现语言是相当容易的。 X Windows系统从API上看本质上是一个单继承的OO系统,但从实现上看却是一团乱七八糟的C语言。
C is not an O-O language under any definition of "O-O" and "language".
It is quite easy to use C as the implementation language for a component that gives an O-O API to its clients. The X Windows system is essentially a single-inheritance O-O system when viewed from its API, but a whole mess of C when viewing its implementation.
令人困惑的可能是,C 可以用来实现面向对象的概念,如多态性、封装等,这可能会让你的朋友相信 C 是面向对象的。问题是,要被视为面向对象的编程语言,需要将这些功能内置到该语言中。但他们不是。
The confusion may be that C can be used to implement object oriented concepts like polymorphism, encapsulation, etc. which may lead your friend to believe that C is object oriented. The problem is that to be considered an object oriented programming language, these features would need to be built into the language. Which they are not.
除非你的朋友谈论的是 Objective C(C 的 OO 超集),否则 C 不是 OO 语言。您可以使用 C 实现 OO 概念(这就是旧的 cfront C++ 编译器所做的,它将 C++ 翻译为 C),但这并不会使 C 成为 OO 语言,因为它没有专门提供对标准 OO 技术(如多态性或封装)的支持。
是的,您可以用 C 语言编写面向对象风格的软件,尤其是自由地(滥用)使用宏,但作为已经看到其中一些尝试的结果的人,我强烈建议使用更适合的语言。
Unless your friend was talking about Objective C (an OO superset of C) then no, C isn't an OO language. You can implement OO concepts using C (that's what the old cfront C++ compiler did, it translated C++ into C) but that doesn't make C an OO language as it doesn't specifically offer support for standard OO techniques like polymorphism or encapsulation.
Yes, you can write software OO style in C, especially with liberal (ab-)use of macros but as someone who has seen the results of some of those attempts, I'd strongly suggest to use a better suited language.
但如果您知道这个技巧,您可以轻松地使用结构体、函数指针和 & 来添加面向对象的功能。自指针。 DirectFB就是这样一个以面向对象的方式编写的C库。坏处是它更容易出错,因为它不受语法和编译类型检查的控制。相反,它基于编码约定。
例如
2 。 C++ 是面向对象的,因为它内置了对面向对象功能(如类和继承)的支持。但有人认为它不是一种完整或纯粹的面向对象语言,因为它确实允许使用 C 语法(结构编程语法)。我还记得 C++ 缺乏一些面向对象的功能,但记不清每一项了。
But if you know the trick you can easily add object oriented capability to it simply using struct, function pointer, & self-pointer. DirectFB is such a C library written in an object oriented way. The bad thing it is more error prone since it is not governed by syntax and compile type checking. It is based on coding convention instead.
e.g.
2 . C++ is object oriented since it has built-in support for object oriented capability like class and inheritance. But there is argument that it is not a full or pure object oriented language since it does allow C syntax (structural programming syntax) in it. I also remember that C++ lack a few object oriented capabilities but not remember each one exactly.
C 不是面向对象的语言。
C 是一种通用的命令式语言,支持结构化编程。
由于 C 不是面向对象的,因此 C++ 的出现是为了具有 OOP 功能,而 OOP 是一种围绕对象组织的编程语言模型。
一种语言要具有OOPs特性,需要实现OOPs的某些原则。其中很少有继承,多态,抽象,封装。
C is not object oriented language.
C is a general-purpose, imperative language, supporting structured programming.
Because C isn't object oriented therefore C++ came into existence in order to have OOPs feature and OOP is a programming language model organized around objects.
A language in order to have OOPs feature needs to implement certain principles of OOPs.Few of them are Inheritance, Polymorphism, Abstraction , Encapsulation.
C是一种基于对象的语言,它不支持面向对象语言的许多特性,如继承、多态性等。
C is a object based language, it does not support many features of object oriented languages such as inheritance, polymorphism etc.
我见过面向对象的Cobol。调用 Cobol 的 Cobol。你想称这些程序员为“真正的”吗?
I have seen Object Oriented Cobol. Cobol that calls Cobol. Do you want to call these programmers "Real"?
C 不是面向对象。
C 不定向到对象。
C++可以。
C is not Object Oriented.
C does not orient to objects.
C++ does.
尽管 C 本身是作为过程语言构建的(它根据过程“思考”:首先执行函数 A,然后将输出传递给函数 B 等。它仅支持“开箱即用”的功能程序流),
可以通过 C 实现 OOP(在 OOP 中,故事是由对象及其职责驱动的,而不是函数及其调用顺序)。
事实上,一些早期的 C++ 实现是将代码转换为一些 C 代码,然后构建它。
但是,有时您必须使用 C(对于不支持 C++ 等的嵌入式设备/GPU 语言)。而你想要 OOP。我建议你查看 COOP - 我的 COOP 轻量级但功能强大的 C 面向对象框架编程。
Though C itself was bulit as procedural language (it "thinks" in terms of procedure: You first execute function A then you pass the output to function B etc. it supports "out of the box" only functional program flow),
It's possible to implement OOP over C (in OOP, the story is driven by Objects and their responsibilities rather than functions and their calling order).
In fact, some early C++ implementations were translating the code to some C code and then building it.
However, sometimes you must use C (for Embedded devices / GPU languages that don't support C++ etc). And you want OOP. I'd suggest you to check out COOP - my C OOP lightweight yet powerful framework for C object-oriented programming.
C 不是面向对象的。 C++ 不是面向对象的。让我解释一下:
面向对象是 Simula 老式事件驱动子集的扩展。真正的面向对象是功能性和反射性的,因为面向对象实际上是一组范例(事件驱动、功能性、反射性)。只有四种语言是真正面向对象的,它们是 Lisp、Smalltalk、Ruby 和 Ocaml。 Perl 落后于两者,因为它没有功能。 Scala 不具有反射性,因此也落后了。 C++ 只是事件驱动,具有一些类似 Simula 的设施,但它是完全结构化的编程语言,并且它不是声明性的,甚至不匹配现实世界。面向对象将现实世界与功能(数学)、事件驱动(对话)和反思(进化)相匹配。 C++ 只有对话。 C++ 不像数学那样具有声明性,也不像生命那样进化。 C++ 只能像人一样交谈。 C++ 就像一个白痴,不知道数学是如何运作的,也不知道生命是如何进化的。
C is not object oriented. C++ is not object oriented. Let me explain:
Object Oriented is an extension to Simula's old fashion event driven subset. Real Object Oriented are functional and reflective because Object Oriented is really a group of paradigms (event driven, functional, reflective). Only four language are really object oriented and these are Lisp,Smalltalk,Ruby and Ocaml. Perl lags between because its not functional. Scala is not reflective so also lags behind. C++ is only event driven with some Simula like facilities but its completely structured programming language and its not declarative or even matchs real world. Object Oriented matchs real world with Functional (Maths), Event Driven (Conversations) and Reflectiveness (Evolution). C++ only has conversations. C++ is not declarative like maths or doesnt evolve like life. C++ only converses like people. C++ is like an idiot that doesnt know how maths work or how life evolves.
不,不是,你的朋友错了。
No, it is not, your friend is wrong.