简体   繁体   English

C的FILE是否具有面向对象的接口?

[英]Does C's FILE have an object-oriented interface?

Does the FILE type used through standard C functions fopen , etc. have an object-oriented interface? 通过标准C函数fopen等使用的FILE类型是否具有面向对象的接口?

I'm looking for opinions with reasoning rather than an absolute answer, as definitions of OO vary by who you ask. 我正在寻找有推理的意见,而不是绝对的答案,因为OO的定义因你提出的问题而异。 What are the important OO concepts it meets or doesn't meet? 它遇到或不满足的重要OO概念是什么?

In response to JustJeff's comment below, I am not asking whether C is an OO language, nor whether C (easily or not) allows OO programming. 回应JustJeff在下面的评论,我不是在问C是否是OO语言,也不是C(容易与否)是否允许OO编程。 (Isn't that a separate issue?) (这不是一个单独的问题吗?)

Is C an object-oriented language? C是面向对象的语言吗?

Was OOP (object-oriented-programming) anything more than a laboratory concept when C and FILE were created? 在创建C和FILE时,OOP(面向对象编程)是否只是一个实验室概念?

Answering these questions will answer your question. 回答这些问题将回答您的问题。

EDIT: 编辑:

Further thoughts: Object Oriented specifically means several behaviors, including: 进一步的想法:面向对象具体意味着几种行为,包括:

Inheritence: Can you derive new classes from FILE? 继承:你能从FILE派生新类吗?

Polymorphism: Can you treat derived classes as FILEs? 多态性:您可以将派生类视为FILE吗?

Encapsulation: Can you put a FILE inside another object? 封装:你能把FILE放在另一个对象里吗?

Methods & Properties: Does a FILE have methods and properties specific to it? 方法和属性: FILE是否具有特定于它的方法和属性? (eg. myFile.Name, myFile.Size, myFile.Delete()) (例如myFile.Name,myFile.Size,myFile.Delete())

Although there are well known C "tricks" to accomplish something resembling each of these behaviors, this is not built in to FILE, and is not the original intent. 虽然有众所周知的C“技巧”来完成类似于这些行为的内容,但这并不是内置于FILE中,并不是最初的意图。

I conclude that FILE is not Object Oriented. 我的结论是FILE不是面向对象的。

If the FILE type were "object oriented", presumably we could derive from it in some meaningful way. 如果FILE类型是“面向对象的”,那么我们可能会以某种有意义的方式从中得出它。 I've never seen a convincing instance of such a derivation. 我从来没有见过这种推导的令人信服的例子。

Lets say I have new hardware abstraction, a bit like a socket, called a wormhole. 可以说我有新的硬件抽象,有点像套接字,称为虫洞。 Can I derive from FILE (or socket) to implement it. 我可以从FILE(或套接字)派生来实现它。 Not really - I've probably got to make some changes to tables in the OS kernel. 不是 - 我可能不得不对OS内核中的表进行一些更改。 This is not what I call object orientation 这不是我所说的面向对象

But this whole issue comes down to semantics in the end. 但这整个问题最终归结为语义。 Some people insist that anything that uses a jump-table is object oriented, and IBM have always claimed that their AS/400 boxes are object-oriented, through & through. 有些人坚持认为使用跳转表的任何东西都是面向对象的,IBM总是声称他们的AS / 400盒子是面向对象的,通过和通过。

For those of you that want to dip into the pit of madness and stupidity that is the USENET comp.object newsgroup, this topic was discussed quite exhaustively there a few years ago, albeit by mad and stupid people. 对于那些想要深入了解USENET comp.object新闻组的疯狂和愚蠢的人来说,这个话题在几年前就已经非常详尽地讨论了,尽管是疯狂和愚蠢的人。 If you want to trawl those depths, the Google Groups interface is a good place to start. 如果您想要搜索这些深度, Google网上论坛界面是一个很好的起点。

No. C is not an object-oriented language. C号不是面向对象的语言。

I know that's an "absolute answer," which you didn't want, but I'm afraid it's the only answer. 我知道这是一个“绝对的答案”,你不想要,但我担心这是唯一的答案。 The reasoning is that C is not object-oriented, so no part of it can have an "object-oriented interface". 原因是C不是面向对象的,因此它的任何部分都不能具有“面向对象的接口”。

Clarification: 澄清:

In my opinion , true object-orientation involves method dispatch through subtype polymorphism. 在我看来 ,真正的面向对象涉及通过子类型多态性的方法调度。 If a language lacks this, it is not object-oriented. 如果一种语言缺乏这一点,它就不是面向对象的。

Object-orientation is not a "technique" like GTK. 面向对象不是像GTK那样的“技术”。 It is a language feature. 这是一种语言功能。 If the language lacks the feature, it is not object-oriented. 如果语言缺少该功能,则它不是面向对象的。

If object-orientation were merely a technique, then nearly every language could be called object-oriented, and the term would cease to have any real meaning. 如果面向对象仅仅是一种技术,那么几乎每种语言都可以称为面向对象,并且该术语将不再具有任何实际意义。

Academically speaking, certainly the actual files are objects . 从学术上讲,当然实际的文件对象 They have attributes and you can perform actions on them. 它们具有属性,您可以对它们执行操作。 Doesn't mean FILE is a class , just saying, there are degrees of OO-ness to think about. 并不意味着FILE是一个 ,只是说,有一定程度的OO-ness需要考虑。

The trouble with trying to say that the stdio FILE interface qualifies as OO, however, is that the stdio FILE interface doesn't represent the 'objectness' of the file very well. 然而,试图说明stdio FILE接口符合OO的问题在于,stdio FILE接口并不能很好地表示文件的“对象性”。 You could use FILEs under plain old C in an OO way, but of course you forfeit the syntactic clarity afforded by Java or C++. 您可以在OO方式下使用普通旧C下的FILE,但当然您会丧失Java或C ++提供的语法清晰度。

It should probably further be added that while you can't generate 'inheritance' from FILE, this further disqualifies it as OO, but you could argue that's more a fault of its environment (plain C) than the abstract idea of the file-as-object itself. 可能还应该进一步补充说,虽然你不能从FILE生成'继承',但这进一步取消了它作为OO的资格,但你可能会认为它更像是环境(普通C)的错误而不是文件的抽象概念 - 如 - 对象本身。

In fact .. you could probably make a case for FILE being something like a java interface. 实际上..你可能为FILE做一个类似java接口的案例。 In the linux world, you can operate almost any kind of I/O device through the open/close/read/write/ioctl calls; 在linux世界中,您可以通过open / close / read / write / ioctl调用操作几乎任何类型的I / O设备; the FILE functions are just covers on top of those; FILE功能只是覆盖在其上的; therefore in FILE you have something like an abstract class that defines the basic operations (open/read/etc) on an 'abstact i/o device', leaving it up to the various sorts of derived types to flesh those out with type-specific behavior. 因此在FILE中你有一个类似于抽象类的东西,它定义了'abstact i / o device'上的基本操作(open / read / etc),将它留给了各种派生类型,以便通过类型特定的方式来实现它们。行为。

Granted, it's very hard to see the OO in a pile of C code, and very easy to break the abstractions, which is why the actual OO languages are so much more popular these days. 当然,很难在一堆C代码中看到OO,并且很容易打破抽象,这就是为什么实际的OO语言如今更受欢迎。

It depends. 这取决于。 How do you define an "object-oriented interface"? 你如何定义“面向对象的界面”? As the comments to abelenky's post shows, it is easy to construct an argument that FILE is object-oriented. 正如对abelenky帖子的评论所示,很容易构造一个FILE是面向对象的论证。 It depends on what you mean by "object-oriented". 这取决于你所说的“面向对象”。 It doesn't have any member methods. 它没有任何成员方法。 But it does have functions specific to it. 但它确实具有特定的功能。

It can not be derived from in the "conventional" sense, but it does seem to be polymorphic. 它不能从“传统”意义上得出,但它似乎确实是多态的。 Behind a FILE pointer, the implementation can vary widely. 在FILE指针后面,实现可以有很大的不同。 It may be a file, it may be a buffer in memory, it may be a socket or the standard output. 它可能是一个文件,它可能是内存中的缓冲区,也可能是套接字或标准输出。

Is it encapsulated? 它被封装了吗? Well, it is essentially implemented as a pointer. 好吧,它基本上是作为指针实现的。 There is no access to the implementation details of where the file is located, or even the name of the file, unless you call the proper API functions on it. 除非您在其上调用适当的API函数,否则无法访问文件所在位置的实现细节,甚至文件名称。 That sounds encapsulated to me. 这听起来像是封装在我身上。

The answer is basically whatever you want it to be. 答案基本上就是你想要的。 If you don't want FILE to be object-oriented, then define "object-oriented" in a way that FILE can't fulfill. 如果您不希望FILE是面向对象的,那么以FILE无法实现的方式定义“面向对象”。

C has the first half of object orientated. C具有面向对象的前半部分。 Encapsulation, ie you can have compound types like FILE* or structs but you can't inherit from them which is the second (although less important) half 封装,即你可以有复合类型,如FILE *或结构,但你不能从它们继承,这是第二个(虽然不太重要)的一半

There are different definitions of oo around. oo有不同的定义。 The one I find most useful is the following (inspired by Alan Kay): 我发现最有用的是以下(灵感来自Alan Kay):

  1. objects hold state (ie references to other objects) 对象保持状态(即对其他对象的引用)
  2. objects receive (and process) messages 对象接收(和处理)消息
  3. processing a message may result in 处理消息可能会导致
    • messages beeing sent to the object itself or other objects 消息发送到对象本身或其他对象
    • a change in the object's state 对象状态的变化

This means you can program in an object-oriented way in any imperative programming language - even assembler. 这意味着您可以在任何命令式编程语言中以面向对象的方式编程 - 甚至是汇编程序。 A purely functional language has no state variables, which makes oo impossible or at least awkward to implement (remember: LISP is not pure!); 一个纯粹的函数式语言没有状态变量,这使得oo不可能或者至少很难实现(记住:LISP 不是纯粹的!); the same should go for purely declarative languages. 对于纯粹的声明性语言也应如此。

In C, message passing in most often implemented as function calls with a pointer to a struct holding the object's state as first argument, which is the case for the file handling api. 在C中,消息传递通常实现为函数调用,其指针指向将对象的状态保存为第一个参数的结构,这是文件处理api的情况。 Still, C as a language can't be classified as oo as it doesn't have syntactic support for this style of programming. 尽管如此,C语言不能归类为oo,因为它没有对这种编程风格的语法支持。

Also, some other definitions of oo include things like class-based inheritance (so what about prototypal languages?) and encapsulation - which aren't really essential in my opinion - but some of them can be implemented in C with some pointer- and casting magic. 另外,oo的其他一些定义包括基于类的继承(那么原型语言怎么样?)和封装 - 这在我看来并不是真正必要的 - 但是其中一些可以用C实现一些指针和转换魔法。

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

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