简体   繁体   English

C语言是面向对象的吗?

[英]Is the C programming language object-oriented?

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.我和一位同事谈论 C 和 C++,他声称 C 是面向对象的,但我声称它不是。 I know that you can do object-oriented-like things in C, but C++ is a true object-oriented language.我知道你可以用 C 做类似面向对象的事情,但 C++ 是一种真正的面向对象语言。

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?你对此有何想法?

If by "is C object oriented?"如果通过“C 是面向对象的吗?” you mean "is C designed with facilities specifically to support object oriented programming?"您的意思是“C 设计的设施是否专门用于支持面向对象的编程?” then, no, C is clearly not object oriented.那么,不,C 显然不是面向对象的。

You can program in an object-orientated style in more or less any language.您可以使用或多或少的任何语言以面向对象的风格进行编程。 (I think runtime polymorphism -- ie virtual methods -- requires a language that supports function pointers.) (我认为运行时多态性——即虚拟方法——需要一种支持函数指针的语言。)

Here are a couple of examples:下面是几个例子:

C isn't object oriented. C 不是面向对象的。 That was the entire purpose behind the ++这就是 ++ 背后的全部目的

As far as a definition of what it takes to be object oriented: check wikipedia .至于什么是面向对象的定义:检查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++..真正的面向对象语言(非决定性)的例子是:Smalltalk、Java、c#、Python、Ruby、C++..

Also, it's possible to have extensions to provide OO features like PHP, Perl, VB (not .Net), ...此外,还可以通过扩展来提供 OO 功能,例如 PHP、Perl、VB(不是 .Net)……

Real programmers can write object-oriented code in ANY language.真正的程序员可以用任何语言编写面向对象的代码。

But no, C is not an 'object-oriented' language.但是不,C 不是“面向对象”语言。 It has no concept of classes, objects, polymorphism, inheritance.它没有类、对象、多态、继承的概念。

Answer can be yes or no , depending on:答案可以是 yes 或 no ,具体取决于:

  • if you ask "is C an object oriented language?"如果你问“C 是一种面向对象的语言吗?” , the answer is "no" because it do not have object oriented constructors, keywords, semantic etc... ,答案是否定的,因为它没有面向对象的构造函数、关键字、语义等......

  • if you intend "can I realize OOP in C?"如果您打算“我可以在 C 中实现 OOP 吗?” , 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. ,答案是肯定的,因为OOP不仅是一种语言的要求,也是一种“思考”的方式,一种编程的方法,在接触某种语言之前。 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(或任何其他并非本机设计为 OOP 的语言)中实现 OOP 肯定会“强制”并且比任何其他 OOP 语言更难以管理,因此也应该有一些限制。

C is not an OO language under any definition of "OO" and "language".在“OO”和“语言”的任何定义下,C 不是 OO 语言。

It is quite easy to use C as the implementation language for a component that gives an OO API to its clients.使用 C 作为组件的实现语言非常容易,该组件为其客户端提供 OO API。 The X Windows system is essentially a single-inheritance OO system when viewed from its API, but a whole mess of C when viewing its implementation.从其 API 来看,X Windows 系统本质上是一个单继承 OO 系统,但从其实现来看,则是一堆 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.混淆可能是 C 可用于实现面向对象的概念,如多态性、封装等,这可能会让您的朋友相信 C 是面向对象的。 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.他们不是。

  1. C is not object oriented in strict sense since it doesn't have a built-in syntax supported object oriented capability like class, inheritance and so on. 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. DirectFB 就是这样一个以面向对象的方式编写的 C 库。 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.它基于编码约定。

eg例如

  IDirectFB/*a typedef of a struct*/ *dfb = NULL;
  IDirectFBSurface/*another typedef of a struct*/ *primary = NULL;
  DirectFBCreate (&dfb); /*factory method to create a struct (e.g. dfb) with 
                         pointers to function and data. This struct is 
                         like an object/instance of a class in a language with build-in 
                         syntax support for object oriented capability  */
  dfb->SetCooperativeLevel/*function pointer*/ 
          (dfb/*self pointer to the object dfb*/, 
           DFSCL_FULLSCREEN);
  dsc.flags = DSDESC_CAPS;
  dsc.caps  = DSCAPS_PRIMARY | DSCAPS_FLIPPING;
  dfb->CreateSurface/*function pointer, also a factory method 
                       to create another object/instance */
          ( dfb/*self pointer to the object dfb*/, 
            &dsc, 
            &primary/*another struct work as object of another class created*/ );
  primary->GetSize/*function pointer*/ 
              (primary/*self pointer to the object primary*/, 
               &screen_width, 
               &screen_height);

2 . 2 . C++ is object oriented since it has built-in support for object oriented capability like class and inheritance. C++ 是面向对象的,因为它内置了对类和继承等面向对象功能的支持。 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.但是有人认为它不是完整的或纯粹的面向对象语言,因为它确实允许使用 C 语法(结构化编程语法)。 I also remember that C++ lack a few object oriented capabilities but not remember each one exactly.我还记得 C++ 缺乏一些面向对象的功能,但不能准确地记住每一个。

Unless your friend was talking about Objective C (an OO superset of C) then no, C isn't an OO language.除非您的朋友在谈论 Objective C(C 的 OO 超集),否则不,C 不是 OO 语言。 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.您可以使用 C 实现 OO 概念(这是旧的 cfront C++ 编译器所做的,它将 C++ 翻译成 C),但这并不使 C 成为一种 OO 语言,因为它没有专门提供对标准 OO 技术(如多态性或封装)的支持。

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.是的,您可以用 C 编写 OO 风格的软件,尤其是在自由(ab-)使用宏的情况下,但作为已经看到其中一些尝试结果的人,我强烈建议使用更适合的语言。

Real programmers can write object-oriented code in ANY language.真正的程序员可以用任何语言编写面向对象的代码。

I have seen Object Oriented Cobol.已经看到Cobol语言的面向对象的。 Cobol that calls Cobol.称为 Cobol 的 Cobol。 Do you want to call these programmers "Real"?你想称这些程序员为“真实的”吗?

C is not object oriented language. C 不是面向对象的语言。 C is a general-purpose, imperative language, supporting structured programming. C 是一种通用的命令式语言,支持结构化编程。 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.因为 C 不是面向对象的,因此 C++ 的出现是为了具有 OOP 功能,而 OOP 是一种围绕对象组织的编程语言模型。 A language in order to have OOPs feature needs to implement certain principles of OOPs.Few of them are Inheritance, Polymorphism, Abstraction , Encapsulation.一门语言为了拥有 OOPs 特性需要实现 OOPs 的某些原则。其中很少有继承、多态、抽象、封装。

C is not Object Oriented . C 不是面向对象的

C does not orient to objects . C 不面向对象

C++ does . 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).尽管 C 本身是作为过程语言构建的(它根据过程“思考”:您首先执行函数 A,然后将输出传递给函数 B 等。它仅支持“开箱即用”的功能程序流),但有可能通过 C 实现 OOP(在 OOP 中,故事由对象及其职责驱动,而不是由函数及其调用顺序驱动)。 In fact, some early C++ implementations were translating the code to some C code and then building it.事实上,一些早期的 C++ 实现将代码转换为一些 C 代码,然后构建它。 However, sometimes you must use C (for Embedded devices / GPU languages that don't support C++ etc).但是,有时您必须使用 C(对于不支持 C++ 等的嵌入式设备/GPU 语言)。 And you want OOP.而你想要 OOP。 I'd suggest you to check out COOP - my C OOP lightweight yet powerful framework for C object-oriented programming.我建议您查看COOP - 我的 C OOP 轻量级但功能强大的 C 面向对象编程框架。

C 是一种基于对象的语言,它不支持面向对象语言的许多特性,如继承、多态等。

C is not object oriented. C 不是面向对象的。 C++ is not object oriented. C++ 不是面向对象的。 Let me explain: Object Oriented is an extension to Simula's old fashion event driven subset.让我解释一下:面向对象是 Simula 旧时尚事件驱动子集的扩展。 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.只有四种语言是真正面向对象的,它们是 Lisp、Smalltalk、Ruby 和 Ocaml。 Perl lags between because its not functional. Perl 介于两者之间,因为它不起作用。 Scala is not reflective so also lags behind. Scala 不是反射性的,所以也落后了。 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. C++ 只是使用一些类似 Simula 的工具来驱动事件,但它是完全结构化的编程语言,它不是声明式的,甚至不匹配现实世界。 Object Oriented matchs real world with Functional (Maths), Event Driven (Conversations) and Reflectiveness (Evolution).面向对象将现实世界与功能(数学)、事件驱动(对话)和反射(进化)相匹配。 C++ only has conversations. C++ 只有对话。 C++ is not declarative like maths or doesnt evolve like life. C++ 不像数学那样是声明式的,也不像生命一样进化。 C++ only converses like people. C++ 只像人一样交谈。 C++ is like an idiot that doesnt know how maths work or how life evolves. C++ 就像一个不知道数学如何运作或生命如何进化的白痴。

不,不是,你的朋友错了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM