使用ANSI-C实现面向对象编程

需积分: 50 13 下载量 79 浏览量 更新于2024-12-21 收藏 1.2MB PDF 举报
"Object-Oriented Programming With ANSI-C" 在编程世界中,面向对象编程(Object-Oriented Programming,OOP)被视为一种解决方案,尽管它已有超过十年的历史。它的核心理念是将我们学习了二十多年的良好编程原则付诸实践。C++、Eiffel、Oberon-2、Smalltalk等语言之所以被视为“新”语言,是因为它们支持面向对象,但并不意味着必须如此使用。即便不具备面向对象特性,程序员依然可以使用普通的ANSI-C实现相同的效果。面向对象的关键在于代码复用,这使得不同项目间能够共享代码,而子程序的概念自计算机诞生之初就存在,优秀的程序员始终会携带他们的工具包和库。 本书《Object-Oriented Programming With ANSI-C》并不旨在吹捧面向对象编程或批判传统方法。相反,它将利用ANSI-C来探索如何实现面向对象编程,理解其技术手段,并揭示这些技术如何帮助我们解决复杂问题。通过使用ANSI-C,读者可以了解面向对象编程的基本概念,如封装、继承和多态,这些都是OOP的核心原则。 封装是将数据和操作数据的方法结合在一起,形成一个独立的单元,这样可以保护数据不受外部直接干扰,提高代码的安全性。在ANSI-C中,可以通过结构体和函数来模拟封装,将数据和操作数据的函数打包在一起。 继承则是允许创建新的类(子类),这些子类从已存在的类(父类)那里继承属性和行为。在ANSI-C中,虽然没有内置的继承机制,但可以通过指针和结构体嵌套来实现类似的功能。子类可以扩展或修改父类的功能,实现代码重用和模块化设计。 多态允许不同的对象对同一消息做出不同的响应,增强了代码的灵活性。在ANSI-C中,多态通常通过函数指针实现,一个函数指针可以指向执行不同功能的函数,根据上下文调用相应的实现。 此外,书中可能还会讨论接口的概念,虽然ANSI-C不直接支持接口,但可以使用抽象数据类型和函数指针来模拟接口,确保不同组件之间的交互遵循统一标准。 通过这种方式,开发者可以利用ANSI-C的简洁性和效率,同时享受到面向对象编程带来的好处。学习如何在没有内置OOP支持的语言中实现这些概念,不仅能够提升编程技巧,也对理解和使用其他支持OOP的语言大有裨益。这本书对于那些希望在Linux环境中使用C语言进行系统级编程,特别是涉及到SCSI设备编程的开发者来说,是一份宝贵的资源。
2010-11-02 上传
没有任何一种编程技术能解决所有问题; 没有任何一种编程语言只输出正确的结果; 没有任何一个程序员需要从零开始一个项目。 -----这三句似曾相识? 这是一本17年前的书,写的是用纯C来实现OOP。。。 亘古的C,永远的C。。。 关天C,我没有其它要说的了。 关于这个资源,还有一句:原书+源码,全在这儿 我希望10年后我还在读它,看它。。。 No programming technique solves all problems. No programming language produces only correct results. No programmer should start each project from scratch. Object-oriented programming is the current cure-all — although it has been around for much more then ten years. At the core, there is little more to it then finally applying the good programming principles which we have been taught for more then twenty years. C++ (Eiffel, Oberon-2, Smalltalk ... take your pick) is the New Language because it is object-oriented — although you need not use it that way if you do not want to (or know how to), and it turns out that you can do just as well with plain ANSI-C. Only object-orientation permits code reuse between projects — although the idea of subroutines is as old as computers and good programmers always carried their toolkits and libraries with them. This book is not going to praise object-oriented programming or condemn the Old Way. We are simply going to use ANSI-C to discover how object-oriented programming is done, what its techniques are, why they help us solve bigger problems, and how we harness generality and program to catch mistakes earlier. Along the way we encounter all the jargon — classes, inheritance, instances, linkage, methods, objects, polymorphisms, and more — but we take it out of the realm of magic and see how it translates into the things we have known and done all along. I had fun discovering that ANSI-C is a full-scale object-oriented language. To share this fun you need to be reasonably fluent in ANSI-C to begin with — feeling comfortable with structures, pointers, prototypes, and function pointers is a must. Working through the book you will encounter all the newspeak — according to Orwell and Webster a language ‘‘designed to diminish the range of thought’’ — and I will try to