ooc:序言

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.
尽管面向对象编程已经出现了十多年,它目前仍是解决问题的万能药。本质上,除了应用那些我们已经被灌输了二十多年的好的编程法则外,并没有什么新的东西出现。C++(Eiffel(译注:一种“纯”OOPL),Oberon-2,Smalltalk...随你挑吧)是一种新的语言因为他是面向对象的,如果你不需要以面向对象的方式使用C++或者你不打算这样做(亦或者你压根不会),那你就可以放弃了,因为仅使用基础的ANSI-C就可以实现面向对象。只有面向对象才允许不同对象间代码重用,尽管子程序的思想和电脑一样久远,而且好的程序员总是随身带着他们的工具包和库。

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.
这本书不打算推崇面向对象的编程思想也不会批判传统的方法。我们只是简单的使用ANSI-C来探索面向对象编程是怎么实现的,以及其中包含的技术,是怎么帮助我们解决复杂的问题的,还有我们怎么利用其普遍性和程序来尽早的捕获异常。我们将逐渐剥离类/继承/实例/连接/方法/对象/多态等术语的奇幻外表,并以熟悉的方式展现出来。

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 demonstrate how it merely combines all the good programming principles that you always wanted to employ into a coherent approach. As a result, you may well become a more proficient ANSI -C programmer.
我愉快地发现ANSI-C是完全的面向对象语言。为了感受到这份乐趣,读者需要非常熟悉ANSI-C,至少要了解结构/指针/原型和函数指针。通过这本书读者将发现ANSI-C全新的语言世界--按照Orwell 和Webster所说的,语言是“被设计用来减小思维深度的”。我将尽力去证明这只是纯粹地把你平时想融汇贯通的好的编程原则融合到一起。作为收获,你将成为一个更高效的ANSI-C程序员。

The first six chapters develop the foundations of object-oriented programming
with ANSI -C. We start with a careful information hiding technique for abstract data types, add generic functions based on dynamic linkage and inherit code by judicious lengthening of structures. Finally, we put it all together in a class hierarchy that makes code much easier to maintain.
前6章建立面向对象的ANSI-C编程基础。我们从一个周密的抽象数据类型的信息隐藏技术开始,加入基于动态连接的通用函数,再通过精明的结构扩展来继承代码。最后,我们将这些都放到一个类层次结构来使得维护代码变得更加简单。

Programming takes discipline. Good programming takes a lot of discipline, a large number of principles, and standard, defensive ways of doing things right. Programmers use tools. Good programmers make tools to dispose of routine tasks once and for all. Object-oriented programming with ANSI -C requires a fair amount of immutable code — names may change but not the structures. Therefore, in chapter seven we build a small preprocessor to create the boilerplate required. It looks like yet another new object-oriented dialect language (yanoodl perhaps?) but it should not be viewed as such — it gets the dull parts out of the way and lets us concentrate on the creative aspects of problem solving with better techniques. ooc sorry) is pliable: we have made it, we understand it and can change it, and it writes the ANSI -C code just like we would.
编程需要范式。好的编程需要许多范式/大量的原则和标准,以及避免错误的预防性措施。程序员使用工具。优秀的程序员创造工具来一劳永逸地处理那些重复的工作。使用ANSI-C进行面向对象编程需要相当多的不变的代码--名称可能改变但结构不变。因此,在第七章我们搭建了一个轻巧的解释器,以创建我们需要的样板。它更像另外一种新的方言化的面向对象语言(也许是yanoodl)但是它不应该被如此看待,OOC(面向对象程序设计ANSI-C)拓新而出,更让我们专注于用更好的技巧解决创造性的问题。OOC有很强的可塑性,我们设计出它以后,可以理解它并且改变它,以此写出的ANSI-C代码正是我们打算写的。

The following chapters refine our technology. In chapter eight we add dynamic type checking to catch our mistakes earlier on. In chapter nine we arrange for automatic initialization to prevent another class of bugs. Chapter ten introduces delegates and shows how classes and callback functions cooperate to simplify, for example, the constant chore of producing standard main programs. More chapters are concerned with plugging memory leaks by using class methods, storing and loading structured data with a coherent strategy, and disciplined error recovery through a system of nested exception handlers.
接下来的章节中将进一步完善我们的技术。第八章,我们增加了动态类型检查以更早地捕获异常。第九章,我们整理了自动初始化机制以避免BUG。第十章,我们介绍了委托,展现了各种类以及使得互操作简便的回调函数。例如:产生标准主程序的日常事务。更多的章节主要关注于使用具有一致性的类方法存储和加载结构化数据以防止内存泄露,并且通过嵌套异常处理程序系统来实现程序化的故障恢复能力。

Finally, in the last chapter we leave the confines of ANSI -C and implement the obligatory mouse-operated calculator, first for curses and then for the X Window System. This example neatly demonstrates how elegantly we can design and implement using objects and classes, even if we have to cope with the idiosyncrasies of foreign libraries and class hierarchies.
最终,在最后一章节中,我们超越了ANSI-C的局限,实现了一个专用的鼠标操作计算器,首先应用于curses终端,接下来扩展到X Window 系统。这个例子清晰的演示了尽管我们必须应对额外的库和类层次结构,我们仍可以使用类和对象优雅地完成设计和实现。

Each chapter has a summary where I try to give the more cursory reader a rundown on the happenings in the chapter and their importance for future work. Most chapters suggest some exercises; however, they are not spelled out formally,because I firmly believe that one should experiment on one’s own. Because we are building the techniques from scratch, I have refrained from making and using a massive class library, even though some examples could have benefited from it. If you want to understand object-oriented programming, it is more important to first master the techniques and consider your options in code design; dependence on somebody else’s library for your developments should come a bit later.
每个章节的都有一个总结,试着给那些略读的读者一个本章节内容的纲要以及这些内容对将来工作的重要性。大多数章节推荐了一些练习;但是这并不是正式的要求,因为我坚定地认为每个人都应该尝试属于自己的体验。由于我们是从零开始构建技能库的,我已经避免制造和使用庞大的类库,即使一些例子这样做很有利。如果你想更好的理解面向对象程序设计,掌握这些技术,并挑选合适的方法进行代码设计显得尤为重要;依靠其他人的类库来做开发是在未来将是小菜一碟。

The techniques described here grew out of my disenchantment with C++ when I needed object-oriented techniques to implement an interactive programming language and realized that I could not forge a portable implementation in C++. I turned to what I knew, ANSI -C, and I was perfectly able to do what I had to. I have shown this to a number of people in courses and workshops and others have used the methods to get their jobs done. It would have stopped there as my footnote to a fad, if Brian Kernighan and my publishers, Hans-Joachim Niclas and John Wait, had not encouraged me to publish the notes (and in due course to reinvent it all once more). My thanks go to them and to all those who helped with and suffered through the evolution of this book. Last not least I thank my family — and no, object-orientation will not replace sliced bread。

这里所描述的技术出自我对C++的醒悟,当我需要使用面向对象技术去实现交互式编程语言时,我意识到在C++中不能完成一个轻便的实例。我转向我已经掌握的,即ANSI-C,我完全能胜任我必须做的工作。我已经在教学和工作中把这些经验和技术分享给很多人,并且应用这些方法完成了他们的工作。如果没有Brian Kernighan ,我的出版社,Hans-Joachim Niclas和John等等鼓励我出版这些笔记(有机会,在适当的时候会重新组织一次),这些将仅仅是我个人拥有的冷落在角落的注释,我感谢他们以及帮助我完善这本书的人。最后,感谢我的家人——并且,不,面向对象取代不了“切片面包”。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 199,636评论 5 468
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 83,890评论 2 376
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 146,680评论 0 330
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 53,766评论 1 271
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 62,665评论 5 359
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,045评论 1 276
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,515评论 3 390
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,182评论 0 254
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,334评论 1 294
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,274评论 2 317
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,319评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,002评论 3 315
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,599评论 3 303
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,675评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,917评论 1 255
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,309评论 2 345
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 41,885评论 2 341

推荐阅读更多精彩内容

  • 毕业后的L先生跟我仿佛没什么变化,我们仍像学生时代样会到学校散步,会去食堂打三个菜让阿姨加饭,他端饭菜和汤,我拿汤...
    柯润梅阅读 315评论 0 0
  • “这世间的事,有因就有果。给你起名叫璞玉,意在告诉你自己是一块可以被雕琢的玉。但雕成什么样子,就要靠你自己了。”外...
    鹿曰曰阅读 384评论 0 3
  • 【Day1 orange】 今天读了钱钟书先生的《围城》前两章,《围城》的时代背景是在中国遭受日本侵略的时期...
    王橘子啦阅读 310评论 0 1
  • 她说 你有没有被人诅咒过 你怎么还不去死 你活在这个世界上有什么意义 她说 你有没有被人抛弃过 大冬天 出了车祸...
    未天阅读 231评论 0 1