难道C'S FILE有一个面向对象的接口? [英] Does C's FILE have an object-oriented interface?

查看:103
本文介绍了难道C'S FILE有一个面向对象的接口?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请问文件类型通过标准的C函数使用的fopen 等有一个面向对象的接口<? / p>

我在寻找与推理,而不是一个绝对的答案的意见,作为OO的定义,由谁你问不同而不同。什么是符合或重要的面向对象的概念不符合?

在响应低于JustJeff的评论,我不要求C是否是一个面向对象的语言,也不管C(容易或不)允许面向对象编程。 (这不是一个单独的问题?)


解决方案

在学术上来讲,肯定的实际文件的是对象的。他们有属性,你可以对其进行操作。并不意味着的文件的是的,只是说,有度OO岬的思考。

,试图说,标准输入输出文件接口有资格作为OO麻烦,然而,就是标准输入输出文件界面不会重新present该文件的对象性非常好。你可以使用下普通的老C文件在OO的方式,当然你放弃通过Java或C ++提供的语法清晰。

这也许应该进一步补充说,虽然你不能生成文件继承,这进一步不够格作为OO,但你可能会说,更重要的是它的环境(纯C)比的抽象观念故障文件为对象本身。

其实..你可以的可能的做出FILE是像Java接口的情况。在Linux世界中,您可以通过打开/关闭/读/写/ ioctl调用运行几乎任何类型的I / O设备的;该文件的功能只是涵盖那些之上;因此,在文件中,您有(打开/读取/ etc)的abstact I / O设备在这上面定义的基本操作一个抽象类,留到各种各样的导出的类型的充实了那些与特定类型的行为。

当然,这是很难看到在一堆C code的面向对象的,而且很容易打破了抽象,这就是为什么实际的面向对象的语言都这么多受追捧。

Does the FILE type used through standard C functions fopen, etc. have an object-oriented interface?

I'm looking for opinions with reasoning rather than an absolute answer, as definitions of OO vary by who you ask. What are the important OO concepts it meets or doesn't meet?

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. (Isn't that a separate issue?)

解决方案

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.

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. 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++.

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.

In fact .. you could probably make a case for FILE being something like a java interface. In the linux world, you can operate almost any kind of I/O device through the open/close/read/write/ioctl calls; the FILE functions are just covers on top of those; 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.

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'S FILE有一个面向对象的接口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆