ANSI-C实现面向对象编程:理解OO技术与代码复用

需积分: 50 3 下载量 105 浏览量 更新于2024-07-20 收藏 1.2MB PDF 举报
"Object-oriented programming (OOP) with ANSI-C: A Practical Guide" 本书旨在深入探讨在ANSI-C语言环境下实现和应用面向对象编程(Object-Oriented Programming,OOP)的理念和技术。尽管C++因其全面的OOP特性而被认为是新语言的代表,如Eiffel、Oberon-2和Smalltalk,但作者强调即使在不完全依赖于这些特性的情况下,ANSI-C也能有效地支持OOP。本书的核心观点并非倡导或贬低传统的编程方法,而是通过实际操作,揭示ANSI-C如何支持OOP原则,例如封装、继承和多态。 首先,章节会从预览部分介绍为何OOP在当前被广泛认为是解决复杂问题的灵丹妙药。尽管它已经存在了超过十年,其核心理念还是在于将二十多年来的良好编程实践融入其中。作者指出,真正的关键在于将这些原则运用到实际项目中,而不是单纯依赖语言特性。 书中强调,虽然OOP的一个主要优点是代码重用,因为它允许在不同项目之间共享模块化的代码,但这并不是只有OOP语言才能实现的。实际上,即使是基础的ANSI-C也可以通过类和结构体来实现某种程度的代码复用,通过函数指针或回调机制来模拟子程序的概念。 在接下来的内容中,读者可以期待学习如何在ANSI-C中定义类和对象,以及如何创建和管理类的实例。会深入剖析封装(encapsulation),即隐藏对象内部细节并提供接口与外部交互,这是OOP的重要基石。同时,书里也会涉及继承(inheritance),即一个类可以从另一个类继承属性和行为,以及多态(polymorphism),即相同的行为在不同的对象上表现出不同的效果。 此外,还会讨论设计模式,这是OOP中解决问题的通用策略,它们可以帮助开发者构建更加灵活和可维护的代码结构。在ANSI-C中,虽然没有像C++那样的内置支持,但可以通过组合和自定义来实现类似的效果。 本书是一本实用性很强的教程,旨在帮助读者掌握如何在 ANSI-C 中有效地实践OOP,理解其背后的理念,并将其应用于实际项目中,实现代码复用和提高软件质量。无论你是C++开发者,还是希望了解如何在不依赖高级特性的语言中实现OOP,这本书都将为你提供有价值的指导。
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