﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>BlogJava-桔子园</title><link>http://www.blogjava.net/orangelizq/</link><description>orangelizq</description><language>zh-cn</language><lastBuildDate>Sat, 11 Oct 2008 16:13:31 GMT</lastBuildDate><pubDate>Sat, 11 Oct 2008 16:13:31 GMT</pubDate><ttl>60</ttl><item><title>[转]编程之道</title><link>http://www.blogjava.net/orangelizq/archive/2008/09/16/229171.html</link><dc:creator>桔子汁</dc:creator><author>桔子汁</author><pubDate>Tue, 16 Sep 2008 07:02:00 GMT</pubDate><guid>http://www.blogjava.net/orangelizq/archive/2008/09/16/229171.html</guid><wfw:comment>http://www.blogjava.net/orangelizq/comments/229171.html</wfw:comment><comments>http://www.blogjava.net/orangelizq/archive/2008/09/16/229171.html#Feedback</comments><slash:comments>1</slash:comments><wfw:commentRss>http://www.blogjava.net/orangelizq/comments/commentRss/229171.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/orangelizq/services/trackbacks/229171.html</trackback:ping><description><![CDATA[编程之道 <br />
<br />
The Silent Void <br />
Book One <br />
Thus spake the master programmer: <br />
编程大师如是说：<br />
<br />
"When you have learned to snatch the error code from the trap frame, it will be time for you to leave." <br />
&#8220;当你从我手中夺走水晶球时，就是你离开的时候了。&#8221; <br />
<br />
1.1 <br />
Something mysterious is formed, born in the silent void. Waiting alone and unmoving, it is at once still and yet in constant motion. It is the source of all programs. I do not know its name, so I will call it the Tao of Programming. <br />
寂静的虚空里诞生了神秘的东西，这种东西恒久存在永不消失，它是所有程序的根源所在，我不知道怎么形容它，姑且称它为编程之道。 <br />
<br />
If the Tao is great, then the operating system is great. If the operating system is great, then the compiler is great. If the compiler is greater, then the applications is great. The user is pleased and there is harmony in the world. <br />
如果道是完美的，那么操作系统就是完美的，如果操作系统是完美的，那么编译嚣就是完美的，如果编译嚣是完美的，那么应用程序就是完美的，所以用户心满意足，整个世界因此和谐。 <br />
<br />
The Tao of Programming flows far away and returns on the wind of morning. <br />
编程之道去如黄鹤来如晨风。 <br />
<br />
1.2 <br />
The Tao gave birth to machine language. Machine language gave birth to the assembler. <br />
道生机器语言，机器语言生汇编嚣。 <br />
<br />
The assembler gave birth to the compiler. Now there are ten thousand languages. <br />
汇编器生编译器，最后产生上万种高级语言。 <br />
<br />
Each language has its purpose, however humble. Each language expresses the Yin and Yang of software. Each language has its place within the Tao. <br />
不论多么的微不足道，每种语言都有它自己的目的，每种语言都表达了软件的阴阳两极。每种语言都各得其道。 <br />
<br />
But do not program in COBOL if you can avoid it. <br />
但是尽量不要用COBOL语言。 <br />
<br />
1.3 <br />
In the beginning was the Tao. The Tao gave birth to Space and Time. Therefore, Space and Time are the Yin and Yang of programming. <br />
道之初，带来了空间和时间，所以，空间和时间是编程的阴阳两极。 <br />
<br />
Programmers that do not comprehend the Tao are always running out of time and space for their programs. Programmers that comprehend the Tao always have enough time and space to accomplish their goals. <br />
不懂编程之道的程序员常常把空间和时间消耗殆尽，得道的程序员则总是有足够的空间和时间去完成编程任务。 <br />
<br />
How could it be otherwise? <br />
否则会是什么样呢？ <br />
<br />
1.4 <br />
The wise programmer is told about the Tao and follows it. The average programmer is told about the Tao and searches for it. The foolish programmer is told about the Tao and laughs at it. <br />
上士闻道，从而行之。中士闻道，谨而寻之。下士闻道，大笑之。 <br />
<br />
If it were not for laughter, there would be no Tao. <br />
大笑不足为道。 <br />
<br />
The highest sounds are the hardest to hear. Going forward is a way to retreat. Greater talent shows itself late in life. Even a perfect program still has bugs. <br />
希音不闻，进即是退，大嚣晚成。任何程序都有漏洞。 <br />
<br />
-------------------------------------------------------------------------------- <br />
<br />
The Ancient Masters <br />
Book Two <br />
Thus spake the master programmer: <br />
编程大师如是说： <br />
　 <br />
"After three days without programming, life becomes meaningless." <br />
三日不编程，食肉无味。 <br />
<br />
2.1 <br />
The programmers of old were mysterious and profound. We cannot fathom their thoughts, so all we do is describe their appearance. <br />
远古时代的编程大师们高深莫测，我们不能揣测他们的所思所想，只能描述外表所见。 <br />
<br />
Aware, like a fox crossing the water. Alert, like a general on the battlefield. Kind, like a hostess greeting her guests. Simple, like uncarved blocks of wood. Opaque, like black pools in darkened caves. <br />
他达明，如狐狸过水；机警，如战场上的将军；和善，如主妇款待客人；简单，呆若木鸡；混沌，如深渊之水。 <br />
<br />
Who can tell the secrets of their hearts and minds? <br />
谁能道尽他们的所有？ <br />
<br />
The answer exists only in the Tao. <br />
答案仅存于道。 <br />
<br />
2.2 <br />
Grand Master Turing once dreamed that he was a machine. When he awoke he exclaimed: <br />
超级大师图灵曾梦见自己是一台机器，醒后他这样回忆： <br />
<br />
"I don&#39;t know whether I am Turing dreaming that I am a machine, or a machine dreaming that I am Turing!" <br />
<br />
&#8220;我不知道是图灵梦见自己变成机器还是机器梦见自己变成图灵。&#8221; <br />
<br />
2.3 <br />
A programmer from a very large computer company went to a software conference and then returned to report to his manager, saying: "What sort of programmers work for other companies? They behaved badly and were unconcerned with appearances. Their hair was long and unkempt and their clothes were wrinkled and old. They crashed out hospitality suites and they made rude noises during my presentation." <br />
一个大公司的程序员参加一个软件会议后向他的主管汇报：&#8220;那些别的公司的程序员都是些什么样的人呀？他们举止不雅，不修边幅，头发蓬乱，衣服破旧，根本不热情好客，还在我说话的时候乱嚷嚷。&#8221; <br />
<br />
The manager said: "I should have never sent you to the conference. Those programmers live beyond the physical world. They consider life absurd, an accidental coincidence. They come and go without knowing limitations. Without a care, they live only for their programs. Why should they bother with social conventions?" <br />
他的主管说：&#8220;我不应该让你参加这次会议，这些程序员生活在现实世界之外。他们认为生活是可笑的，一场意外的偶然而已。他们来去自由，无所牵挂，他们只为他们的程序生活。为什么要用世俗的烦扰去扰乱他们呢？&#8221; <br />
<br />
"They are alive within the Tao." <br />
&#8220;他们生活在道中&#8221;。 <br />
<br />
2.4 <br />
A novice asked the Master: "Here is a programmer that never designs, documents, or tests his programs. Yet all who know him consider him one of the best programmers in the world. Why is this?" <br />
一个初学者问主管经理：&#8220;有一个程序员，他从来不预先设计，也不写文档，甚至不测试他的程序，但是知道他的人都认为他是世界上最伟大的程序员，为什么呢？&#8221; <br />
<br />
The Master replies: "That programmer has mastered the Tao. He has gone beyond the need for design; he does not become angry when the system crashes, but accepts the universe without concern. He has gone beyond the need for documentation; he no longer cares if anyone else sees his code. He has gone beyond the need for testing; each of his programs are perfect within themselves, serene and elegant, their purpose self-evident. Truly, he has entered the mystery of the Tao." <br />
经理说：&#8220;那个程序员掌握了道。他不需要预先进行设计；系统崩溃时他也从不烦燥，只是接受发生的一切而不管发生的事是好是坏 。他不需要写文档，他从不顾及有没有人看他写的代码。他也不需要进行测试；他写的每个程序都有一个完美的自我，平静而优雅，它们的目的不言自明。他已经真正掌握了道的精髓。&#8221; <br />
<br />
-------------------------------------------------------------------------------- <br />
<br />
Design <br />
Book Three <br />
Thus spake the master programmer: <br />
编程大师如是说： <br />
<br />
"When program is being tested, it is too late to make design changes." <br />
<br />
&#8220;程序被测试时再去改变它的设计已经太晚了。&#8221; <br />
<br />
3.1 <br />
There once was a man who went to a computer trade show. Each day as he entered, the man told the guard at the door: <br />
曾经有一个人参加计算机展览，每天他进门时都对门卫说： <br />
<br />
"I am a great thief, renowned for my feats of shoplifting. Be forewarned, for this trade show shall not escape unplundered." <br />
&#8220;先警告你，我是偷盗高手，我入室偷盗的本领闻名遐迩。这次展览会也再劫难逃。&#8221; <br />
<br />
This speech disturbed the guard greatly, because there were millions of dollars of computer equipment inside, so he watched the man carefully. But the man merely wandered from booth to booth, humming quietly to himself. <br />
警卫因此大动干戈，因为里面有价值百万的计算机设备，所以他牢牢盯紧了这个人，但是这个人只是从一个摊位逛到另一个摊位，一边平静地喃喃自语。 <br />
<br />
When the man left, the guard took him aside and searched his clothes, but nothing was to be found. <br />
当这个人离开时，警卫搜了他的身，但是什么也没有发现。 <br />
<br />
On the next day of the trade show, the man returned and chided the guard saying: "I escaped with a vast booty yesterday, but today will be even better." So the guard watched him ever more closely, but to no avail. <br />
展览会的第二天，这个人又对警卫说：&#8220;昨天我满载而归，但是今天我会收获更大。&#8221;所以警卫更加小心地盯紧他，但是这样做完全于事无补。 <br />
<br />
On the final day of the trade show, the guard could restrain his curiosity no longer. "Sir Thief," he said, "I am so perplexed, I cannot live in peace. Please enlighten me. What is it that you are stealing?" <br />
展览会的最后一天，警卫再也不能忍住他的好奇心了。&#8220;小偷先生，&#8221;他说，&#8220; 你说我惊慌不安，请告诉我，你到底偷了什么？&#8221;。 <br />
<br />
The man smiled. "I am stealing ideas," he said. <br />
这个人笑笑说：&#8220;我在偷想法。&#8221; <br />
<br />
3.2 <br />
There once was a master programmer who wrote unstructured programs. A novice programmer, seeking to imitate him, also began to write unstructured programs. When the novice asked the master to evaluate his progress, the master criticized him for writing unstructured programs, saying: "What is appropriate for the master is not appropriate for the novice. You must understand the Tao before transcending structure." <br />
有一位编程大师，他写非结构化的程序，一位初学者刻意模仿他，也写非结构化的程序。当他让大师看他的进步时，大师批评了他的非结构化程序：&#8220; 对一位编程大师合适的东西未必对一个初学者同样合适，在超越结构化之前，你必须理解编程之道。&#8221; <br />
<br />
3.3 <br />
There was once a programmer who was attached to the court of the warlord of Wu. The warlord asked the programmer: "Which is easier to design: an accounting package or an operating system?" <br />
###有人问一位程序员，&#8220;一个财务软件和一个操作系统哪个更容易设计？&#8221; <br />
<br />
"An operating system," replied the programmer. <br />
&#8220;是操作系统&#8221;，这位程序员回答说。 <br />
<br />
The warlord uttered an exclamation of disbelief. "Surely an accounting package is trivial next to the complexity of an operating system," he said. <br />
此人大惑不解。他说：&#8220;显然一个财务软件比起操作系统来说其复杂性是微不足道的&#8221;。 <br />
<br />
"Not so," said the programmer, "when designing an accounting package, the programmer operates as a mediator between people having different ideas: how it must operate, how its reports must appear, and how it must conform to the tax laws. By contrast, an operating system is not limited my outside appearances. When designing an operating system, the programmer seeks the simplest harmony between machine and ideas. This is why an operating system is easier to design." <br />
程序员说：&#8220;不，设计财务软件时，一个程序员必须成为持不同意见的用户与计算机的一个中介，他必须了解用户的操作习惯，报表要是什么形式，如何遵循税法。相反，一个操作系统完全与这些外部的东西无关。设计操作系统，程序员只需要达到自己的设想与机器之间的简单的和谐。这就是为什么操作系统反而比财务软件更容易设计。&#8221; <br />
<br />
The warlord of Wu nodded and smiled. "That is all good and well, but which is easier to debug?" <br />
这些人笑着说。&#8220;不错，但是哪一个更容易调试呢？&#8221; <br />
<br />
The programmer made no reply. <br />
程序员没有回答。 <br />
<br />
3.4 <br />
A manager went to the master programmer and showed him the requirements document for a new application. The manager asked the master: "How long will it take to design this system if I assign five programmers to it?" <br />
一个项目经理带给编程大师一个项目的需求，然后问大师：&#8220;如果我给你5个程序员，要多少时间设计这个项目？&#8221; <br />
<br />
"It will take one year," said the master promptly. <br />
&#8220;一年&#8221;，大师说。 <br />
<br />
"But we need this system immediately or even sooner! How long will it take it I assign ten programmers to it?" <br />
&#8220;但是我们等不了那么长时间，越快越好，如果10程序员呢？&#8221; <br />
<br />
The master programmer frowned. "In that case, it will take two years." <br />
大师皱了一下眉头说：&#8220;那就要花2年&#8221;。 <br />
<br />
"And what if I assign a hundred programmers to it?" <br />
&#8220;那，100年程序员呢？&#8221; <br />
<br />
The master programmer shrugged. "Then the design will never be completed," he said. <br />
大师耸了耸肩说：&#8220;那这个项目就永远完不成了。&#8221; <br />
<br />
-------------------------------------------------------------------------------- <br />
<br />
Coding <br />
Book Four <br />
Thus spake the master programmer: <br />
编程大师如是说： <br />
<br />
"A well-written program is its own heaven; <br />
a poorly-written program is its own hell." <br />
&#8220;写的好的程序是它自己的天堂，写的不好的程序是它自己的地狱&#8221; <br />
<br />
4.1 <br />
A program should be light and agile, its subroutines connected like a strings of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little nor too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity. <br />
一个程序应该是轻灵自由的，它的子过程就象串在一根线上的珍珠。自始至终，目标明确。增之一字节则太长，减之一字节则太短。既没有不必要的循环也没有没有被引用的变量，既不缺少结构化也不至于僵硬呆板。 <br />
<br />
A program should follow the Law of Least Astonishment. What is this law? It is simply that the program should always respond to the user in the way that astonishes him least. <br />
一个程序应该遵循"最小惊讶原则&#39;&#39;，什么是"最小惊讶原则&#39;&#39;？就是说一个程序应该最小程度地引起用户的惊讶。 <br />
<br />
A program, no matter how complex, should act as a single unit. The program should be directed by the logic within rather than by outward appearances. <br />
无论一个程序无论有多么复杂都应该是一个单一的整体。程序是被它的内部逻辑所指引而不是它的外观表现。 <br />
<br />
If the program fails in these requirements, it will be in a state of disorder and confusion. The only way to correct this is to rewrite the program. <br />
如果一个程序不能满足所要的需求，它就会处于混乱无序的状态中。唯一的出路就是重写这个程序。 <br />
<br />
4.2 <br />
A novice asked the master: "I have a program that sometimes runs and sometimes aborts. I have followed the rules of programming, yet I am totally baffled. What is the reason for this?" <br />
一个初学者问大师：&#8220;我的一个程序有时正常有时不正常。我已经完全遵循编程的规则，而且我完全被它弄糊涂了，到底是什么原因会导致这样呢？&#8221; <br />
<br />
The master replied: "You are confused because you do not understand the Tao. Only a fool expects rational behavior from his fellow humans. Why do you expect it from a machine that humans have constructed? Computers simulate determinism; only the Tao is perfect. <br />
大师说：&#8220;你困惑是因为你不了解道。只有傻瓜才会相信只要遵循别人所说就能得到想当然的结果。为什么你要从一个人类自己构造的机器中去得到想当然的结果呢？计算机只是决定论的产物；只有道才是唯一完美的。 <br />
<br />
The rules of programming are transitory; only the Tao is eternal. Therefore you must contemplate the Tao before you receive enlightenment. <br />
任何编程的规则都只适合于特定的情况，只有道才是永恒不变的。所以在你受到指引之前要先去思考道。 <br />
<br />
"But how will I know when I have received enlightenment?" asked the novice. <br />
&#8220;但是我怎么知道我何时受到指引呢？&#8221; <br />
<br />
"Your program will then run correctly," replied the master. <br />
&#8220;当你的程序正确运行时&#8221;。大师说。 <br />
<br />
4.3 <br />
A master was explaining the nature of the Tao to one of his novices, "The Tao is embodied in all software -- regardless of how insignificant," said the master. <br />
初学者请大师解释&#8220;道法自然&#8221;。大师说：&#8220;道存在于任何软件中－－无论是多么没有意义的软件&#8221;。 <br />
<br />
"Is the Tao in a hand-held calculator?" asked the novice. <br />
&#8220;难道也存在于手持式的计算嚣里？&#8221;，初学者问。 <br />
<br />
"It is," came the reply. <br />
&#8220;是的，&#8221; <br />
<br />
"Is the Tao in a video game?" continued the novice. <br />
&#8220;也在游戏机里？&#8221;，初学者又问到。 <br />
<br />
"It is even in a video game," said the master. <br />
&#8220;是的，甚至也存在于游戏机里&#8221;。大师说。 <br />
<br />
"And is the Tao in the DOS for a personal computer?" <br />
&#8220;也存在于个人计算机的DOS里吗？&#8221; <br />
<br />
The master coughed and shifted his position slightly. "The lesson is over for today," he said. <br />
大师咳嗽了一声，稍微动了一下，说，&#8220;今天的课就到这里&#8221;。 <br />
<br />
4.4 <br />
Price Wang&#39;s programmer was coding software. His fingers danced upon the keyboard. The program compiled without an error message, and the program ran like a gentle wind. <br />
###编程大师编写软件时，手指在键盘上快速飞舞。程序编译时没有一条错误信息，程序运行起来就象一阵微风吹过。 <br />
<br />
Excellent!" the Price exclaimed, "Your technique is faultless!" <br />
太精彩了！你的技巧已经无可挑剔了。 <br />
<br />
"Technique?" said the programmer, turning from his terminal, "What I follow is the Tao -- beyond all technique. When I first began to program I would see before me the whole program in one mass. After three years I no longer saw this mass. Instead, I used subroutines. But now I see nothing. My whole being exists in a formless void. My senses are idle. My spirit, free to work without a plan, follows its own instinct. In short, my program writes itself. True, sometimes there are difficult problems. I see them coming, I slow down, I watch silently. Then I change a single line of code and the difficulties vanish like puffs of idle smoke. I then compile the program. I sit still and let the joy of the work fill my being. I close my eyes for a moment and then log off." <br />
&#8220;技巧？&#8221;，大师转过身说，&#8220;我所遵循的是道--它超乎所有的技巧。当我开始编程时我看到的是整个一大块的程序，三年后我看到的是子过程。现在我什么也看不到了。我的整个存在是没有任何形式的虚无。我感觉很悠闲，总之，事实上是我的程序自己在写，有时我看到一些问题，我看到它们，就停下来静静地观察它们，然后我改变了一行代码，难题就象一阵轻烟一样化为乌有。然后我编译程序。坐在那里享受工作的喜悦。闭了一会眼睛然后退出系统。 <br />
<br />
Price Wang said, "Would that all of my programmers were as wise!" <br />
&#8220;什么时候我的程序员才能都达到这样的境界!&#8221; <br />
<br />
<br />
Mainteance <br />
Book Five <br />
Thus spake the master programmer: <br />
编程大师如是说： <br />
<br />
"Though a program be but three lines long, someday it will have to be maintained." <br />
&#8220;既使一个程序只有三行长，也总有一天需要去维护它&#8221; <br />
<br />
5.1 <br />
A well-used door needs no oil on its hinges. <br />
A swift-flowing steam does no grow stagnant. <br />
Neither sound nor thoughts can travel through a vacuum. <br />
Software rots if not used. <br />
<br />
经常使用的门不需要往门轴里上油。 <br />
流动的东西就不会停滞不前。 <br />
流水不腐，户枢不蠹。--王磊 <br />
声音和思想都不能在真空里传播。 <br />
软件不用就会腐朽。 <br />
<br />
These are great mysteries. <br />
<br />
<br />
5.2 <br />
A manager asked a programmer how long it would take him to finish the program on which he was working. "I will be finished tomorrow," the programmer promptly replied. <br />
经理问程序员要多长时间才能完成他的项目。&#8220;明天&#8221; <br />
<br />
"I think you are being unrealistic," said the manager. "Truthfully, how long will it take?" <br />
&#8220;太不着边际了，老实说，要多久？&#8221; <br />
<br />
The programmer thought for a moment. "I have some features that I wish to add. This will take at least two weeks," he finally said. <br />
程序员想了一想说。&#8220;还有一些新的功能要加进去，可能至少要两个星期吧。&#8221; <br />
<br />
"Even that is too much to expect," insisted the manager, "I will be satisfied if you simply tell me when the program is complete." <br />
&#8220;即使两个星期恐怕也太夸张了，什么时候你只要告诉我说程序已经完成就好了。&#8221; <br />
<br />
The programmer agreed to this. <br />
<br />
Several years slated, the manager retired. On the way to his retirement lunch, he discovered the programmer asleep at his terminal. He had been programming all night. <br />
几年后，这个经理已经退休了，在他的离职午餐上，他发现这些程序员在他的终端前睡着了，他整夜都在编程。 <br />
<br />
5.3 <br />
A novice programmer was once assigned to code a simple financial package. <br />
一个初学者被要求编写一个财务软件。 <br />
<br />
<br />
The novice worked furiously for many days, but when his master reviewed his program, he discovered that it contained a screen editor, a set of generalized graphics routines, and artificial intelligence interface, but not the slightest mention of anything financial. <br />
他疯狂地工作了很多天，但他的主管检视他的程序时发现，它写了一个编辑嚣，一个图形程序集，和人工智能的界面，但是看不到任何跟财务有关的东西。 <br />
<br />
When the master asked about this, the novice became indignant. "Don&#39;t be so impatient," he said, "I&#39;ll put the financial stuff in eventually." <br />
主管要求解释时，程序员被激怒了：&#8220;你太没耐心了，我会在最后写财务的部分。&#8221; <br />
<br />
5.4 <br />
Does a good farmer neglect a crop he has planted? <br />
Does a good teacher overlook even the most humble student? <br />
Does a good father allow a single child to starve? <br />
Does a good programmer refuse to maintain his code? <br />
<br />
一个好的农民不会不管他的庄稼。 <br />
一个好的老师不会不管哪怕是最差的学生。 <br />
一个好的父亲不会让他的任何一个孩子挨饿。 <br />
一个好的程序员不应拒绝维护他的程序。 <br />
<br />
-------------------------------------------------------------------------------- <br />
<br />
Management <br />
Book Six <br />
Thus spake the master programmer: <br />
编程大师如是说： <br />
<br />
"Let the programmer be many and the managers few -- then all will be productive." <br />
&#8220;增加程序员，减少经理--这样他们都可提高效率。&#8221; <br />
<br />
6.1 <br />
When managers hold endless meetings, the programmers write games. When accountants talk of quarterly profits, the development budget is about to be cut. When senior scientists talk blue sky, the clouds are about to roll in. <br />
经理们忙于无休无止的会议时，程序员们在写游戏。财务主管在谈论季度盈利时开发预算将会被削减。当老科学家在谈天空的蓝色时，云团蜂拥而至。 <br />
<br />
Truly, this is not the Tao of Programming. <br />
这不是编程之道。 <br />
<br />
When managers make commitments, game programs are ignored. When accountants make long-range plans, harmony and order are about to be restored. When senior scientists address the problems at hand, the problems will soon be solved. <br />
经理分配任务时不会包括游戏，会计师做长期计划时，应该做到面面俱到，皆大欢喜。科学家提出一个问题，就会很快得到解决。 <br />
<br />
Truly, this is the Tao of Programming. <br />
这才是编程之道。 <br />
<br />
6.2 <br />
Why are programmers non-productive? Because their time is wasted in meetings. <br />
为什么程序员没有效率，因为他们把时间都浪费在开会上了。 <br />
<br />
Why are programmers rebellious? Because the management interferes too much. <br />
为什么程序员难于管理？因为管理者的干预太多了。 <br />
<br />
Why are the programmers resigning one by one? Because they are burnt out. <br />
为什么程序员一个接一个地辞职，因为他们累坏了。 <br />
<br />
Having worked for poor management, they no longer value their jobs. <br />
在糟糕的管理下工作，他们享受不到工作的乐趣。 <br />
<br />
6.3 <br />
A manager was about to be fired, but a programmer who worked for him invented a new program that became popular and sold well. As a result, the manager retained his job. <br />
一个经理将被解雇，但他手下的一个程序员写了一个流行的软件并且销路很旺。所以经理保住了他的工作。 <br />
<br />
The manager tried to give the programmer a bonus, but the programmer refused it, saying, "I wrote the program because I though it was an interesting concept, and thus I expect no reward." <br />
经理为程序员分红，但程序员拒绝了，他说：&#8220;我写程序是因为写程序很有趣，所以我并没想过得资金&#8221;。 <br />
<br />
The manager, upon hearing this, remarked, "This programmer, though he holds a position of small esteem, understands well the proper duty of an employee. Lets promote him to the exalted position of management consultant!" <br />
这位经理说，&#8220;这位程序员，不管他现在是什么位置，他都能理解一个雇员的职责所在。我们要把他提升为管理顾问&#8221;。 <br />
<br />
But when told this, the programmer once more refused, saying, "I exist so that I can program. If I were promoted, I would do nothing but waste everyone&#39;s time. Can I go now? I have a program that I&#39;m working one." <br />
这位程序员又一次拒绝了，他说：&#8220;我是一个程序员所以我可以编程，如果我被提升了，除了浪费别人的时间什么也做不了，现在我可以走了吗？我还有一个程序没写完。&#8221; <br />
<br />
6.4 <br />
A manger went to his programmers and told them: "As regards to your work hours: you are going to have to come in at nine in the morning and leave at five in the afternoon." At this, all of them became angry and several resigned on the spot. <br />
经理对程序员说，&#8220;你们的工作时间是早上9点到正午点。&#8221;，所有的程序员都很不满。 <br />
<br />
So the manager said: "All right, in that case you may set your own working hours, as long as you finish your projects on schedule." The programmers, now satisfied, began to come in a noon and work to the wee hours of the morning. <br />
经理又说：&#8220;好吧，那随你们的便，只要能按时完成任务。&#8221;，程序员们这下满意了，他们中午上班，一直工作到凌晨。 <br />
<br />
-------------------------------------------------------------------------------- <br />
<br />
Corporate Wisdom <br />
Book Seven <br />
Thus spake the master programmer: <br />
编程大师如是说： <br />
<br />
"You can demonstrate a program for a corporate executive, but you can&#39;t make him computer literate." <br />
<br />
### <br />
&#8220;你可以向一位公司主管演示一套程序，但你不能让他的电脑变得象人一样。&#8221; <br />
<br />
7.1 <br />
A novice asked the master: "In the east there is a great tree-structure that men call &#39;Corporate Headquarters&#39;. It is bloated out of shape with vice-presidents and accountants. It issues a multitude of memos, each saying &#39;Go, Hence!&#39; or &#39;Go, Hither!&#39; and nobody knows what is meant. Every year new names are put onto the branches, but all to no avail. How can such an unnatural entity exist?" <br />
一个新学问大师：&#8220;在东方，有一个庞大的机构，人们称作总部。它由为数众多的大小领导。每天发出大量的备忘录，每个备忘录都说：&#8216;干这个，干那个&#8217;，没有一个人知道它是什么意思。每年这个机构都会有更多的人加进来而变得越来越大，但是去没有人去做事情。怎么烩样奇怪的东西还能存在呢？&#8221; <br />
<br />
The master replies: "You perceive this immense structure and are disturbed that it has no rational purpose. Can you not take amusement from its endless gyrations? Do you not enjoy the untroubled ease of programming beneath its sheltering branches? Why are you bothered by its uselessness?" <br />
大师说：&#8220;你已经知道这个巨大的机构并不合理，你就不能不管它为什么这样吗？你尽管享受它的好处就是了，干吗要去管它到底是没有效率呢？&#8221; <br />
<br />
7.2 <br />
In the east there is a shark which is larger than all other fish. It changes into a bird whose winds are like clouds filling the sky. When this bird moves across the land, it brings a message from Corporate Headquarters. This message it drops into the midst of the program- mers, like a seagull making its mark upon the beach. Then the bird mounts on the wind and, with the blue sky at its back, returns home. <br />
在遥远的东方，传说有一只巨大的鱼，叫鲲，它变成了一只大鸟，叫鹏，鹏的翅膀可以遮天蔽日，这只大鸟经过陆地的时候，它带来了一个消息，它把这个消息丢在程序员中，就象一只海鸥在海滩上做的记号，然后鹏背负蓝天，乘风而上，回家去了。 <br />
<br />
The novice programmer stares in wonder at the bird, for he understands it not. The average programmer dreads the coming of the bird, for he fears its message. The master programmer continues to work at his terminal, for he does not know that the bird has come and gone. <br />
初学者惊恐地看着这只鸟，根本不敢相信，中级程序员担心这只鸟的到来，因为它害怕它带来的消息，编程大师则继续在他的终端前工作他根本不知道这只鸟曾经来过。 <br />
<br />
7.3 <br />
The Magician of the Ivory Tower brought his latest invention for the master programmer to examine. The magician wheeled a large black box into the master&#39;s office while the master waited in silence. <br />
一个学究带着他的最新发明去见编程大师，他把一个大黑盒子推进大师的办公室。 <br />
<br />
"This is an integrated, distributed, general-purpose workstation," began the magician, "ergonomically designed with a proprietary operating system, sixth generation languages, and multiple state of the art user interfaces. It took my assistants several hundred man years to construct. Is it not amazing?" <br />
&#8220;这是综合的、分布式的通用工作站，&#8221;，学究开始介绍了，&#8220;按人体工学设计的操作系统，第六代的高级语言，多样的用户界面，整整花了我的助手好几百人年的时间，挺不错吧？&#8221; <br />
<br />
The master raised his eyebrows slightly. "It is indeed amazing," he said. <br />
大师扬了一下眉毛，说：&#8220;的确很了不起。&#8221; <br />
<br />
"Corporate Headquarters has commanded," continued the magician, "that everyone use this workstation as a platform for new programs. Do you agree to this?" <br />
&#8220;头儿已经说了，&#8221;这位学究继续说，&#8220;每个人都要用这个工作站作平台，你觉得怎么样？&#8221; <br />
<br />
"Certainly," replied the master, "I will have it transported to the data center immediately!" And the magician returned to his tower, well pleased. <br />
&#8220;当然了，&#8221;，大师回答说，&#8220;我要马上把它送到数据中心去！&#8221;，这位学究满意去回去了。 <br />
<br />
Several days later, a novice wandered into the office of the master programmer and said, "I cannot find the listing for my new program. Do you know where it might be?" <br />
几天后，一个新手走进大师的办公室说，&#8220;我找不到我新写的程序了，你知道它可能会在哪儿吗？&#8221; <br />
<br />
"Yes," replied the master, "the listings are stacked on the platform in the data center." <br />
&#8220;当然，&#8221;，大师说，&#8220;你的程序在数据中心的平台里放着。&#8221; <br />
<br />
7.4 <br />
The master programmer moves from program to program without fear. No change in management can harm him. He will not be fired, even if the project is canceled. Why is this? He is filled with the Tao. <br />
编程大师从一个项目转到另一个项目而豪无顾虑，任何管理上的变动都不能影响他，他不会被解雇，即使项目被取消了。为什么呢？他了解了道。 <br />
<br />
-------------------------------------------------------------------------------- <br />
<br />
Hardware and Software <br />
Book Eight <br />
Thus spake the master programmer: <br />
<br />
　 <br />
"Without the wind, the grass does not move. <br />
Without software, hardware is useless." <br />
风不动则草不动， <br />
没有软件，硬件只是一堆发热的电子器件 <br />
<br />
8.1 <br />
A novice asked the master: "I perceive that one computer company is much larger than all others. It towers above its competition like a giant among dwarfs. Any one of its divisions could comprise an entire business. Why is this so?" <br />
一个初学者问编程大师：&#8220;我知道有一家计算机公司比所有其它的公司都大。在它面前它的竞争对手就象站在巨人面前的侏儒。它的任一部门都有各自的业务，为什么会这样呢？&#8221; <br />
<br />
The master replied, "Why do you ask such foolish questions? That company is large because it is so large. If it only made hardware, nobody would buy it. If it only maintained systems, people would treat it like a servant. But because it combines all of these things, people think it one of the gods! By not seeking to strive, it conquers without effort." <br />
大师说，&#8220;为什么你问如此愚蠢的问题呢？那家公司大是因为它就是如此之大，如果它只做硬件，就没有人会买，如果它只维护系统，人们只会把它当作仆人，但是它的业务囊括了所有这些东西，人们就对它顶礼莫拜好象不用努力奋斗就可击败竞争对手。" <br />
<br />
8.2 <br />
A master programmer passed a novice programmer one day. The master noted the novice&#39;s preoccupation with a hand-held computer game. "Excuse me", he said, "may I examine it?" <br />
有一天编程大师碰见了一个正玩掌上游戏机的编程新手，他说：&#8220;给我看一下？&#8221; <br />
<br />
The novice bolted to attention and handed the device to the master. "I see that the device claims to have three levels of play: Easy, Medium, and Hard", said the master. "Yet every such device has another level of play, where the device seeks not to conquer the human, nor to be conquered by the human." <br />
新手把游戏机递给大师，&#8220;我看到上面说明了三种难度的玩法：容易，中等，高难&#8221;，大师说，&#8220;但是每个这样的游戏机都有另外一种玩法，此时游戏机即不是想着怎么赢你，也不是怎么被你赢。&#8221; <br />
<br />
"ray, great master," implored the novice, "how does one find this mysterious setting?" <br />
&#8220;那么请问大师&#8221;，初学者说，&#8220;这种玩法到底是怎样玩的呢？&#8221; <br />
<br />
The master dropped the device to the ground and crushed it under foot. And suddenly the novice was enlightened. <br />
大师把游戏机摔到地下然后用脚踩碎它，突然初学者明白了。 <br />
<br />
8.3 <br />
There was once a programmer who worked upon microprocessors. "Look at how well off I am here," he said to a mainframe programmer who came to visit, "I have my own operating system and file storage device. I do not have to share my resources with anyone. The software is self-consistent and easy-to-use. Why do you not quit your present job and join me here?" <br />
有一个工作在微机上的程序员对前来参观的大型机程序员说：&#8220;看看我这里吧，我有自己的操作系统和文件存储设备，我不需要与别人共享任何资源，软件条条有理并且易于操作。为什么你不辞去你的工作到这里来呢？&#8221; <br />
<br />
The mainframe programmer then began to describe his system to his friend, saying: "The mainframe sits like an ancient sage meditating in the midst of the data center. Its disk drives lie end-to-end like a great ocean of machinery. The software is a multi-faceted as a diamond and as convoluted as a primeval jungle. The programs, each unique, move through the system like a swift-flowing river. That is why I am happy where I am." <br />
大型机的程序员对他的朋友说：&#8220;大型机就象古代圣贤一样深思熟虑，它的磁盘设备从头到尾就象是一个巨大的机械设备的海洋。软件象钻石一样有多个面，象远古的森林一样令人费解。每一个程序都象水经过河流一样穿行于系统中，那就是为什么我乐于为它工作&#8221; <br />
<br />
The microcomputer programmer, upon hearing this, fell silent. But the two programmers remained friends until the end of their days. <br />
微机程序员听了这些话后，默默不语，但这两个程序员至死都是很好的朋友 <br />
<br />
8.4 <br />
Hardware met Software on the road to Changtse. Software said: "You are the Yin and I am the Yang. If we travel together we will become famous and earn vast sums of money." And so the pair set forth together, thinking to conquer the world. <br />
硬件和软件在路上碰见了，软件说：&#8220;你是阴我是阳，如果我们联手就可以赚大钱。&#8221;所以它们走到了一直，考虑怎样征服世界。 <br />
<br />
Presently, they met Firmware, who was dressed in tattered rags, and hobbled along propped on a thorny stick. Firmware said to them: "The Tao lies beyond Yin and Yang. It is silent and still as a pool of water. It does not seek fame, therefore nobody knows its presence. It does not seeks fortune, for it is complete within itself. It exists beyond space and time." <br />
不久，它们遇到了固件，固件衣衫褴褛，拄着一根拐棍步履蹒跚。固件对它们说：&#8220;道在于阴阳之外。它静如止水，它从不指望能扬名远近，所以没有人知道它的存在。它也不寻求幸福，它只是与它自己的存在在一起，它存在于空间和时间之外。&#8221; <br />
<br />
Software and Hardware, ashamed, returned to their homes. <br />
软件和硬件面有惭色，各自回了家。 <br />
Epilogue <br />
Book Nine <br />
Thus spake the master programmer: <br />
编程大师如是说： <br />
<br />
"Time for you to leave." <br />
&#8220;现在是你出师的时候了。&#8221; <br />
_________________<br />
<img src ="http://www.blogjava.net/orangelizq/aggbug/229171.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/orangelizq/" target="_blank">桔子汁</a> 2008-09-16 15:02 <a href="http://www.blogjava.net/orangelizq/archive/2008/09/16/229171.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>[转]Java泛型编程指南</title><link>http://www.blogjava.net/orangelizq/archive/2008/09/16/229158.html</link><dc:creator>桔子汁</dc:creator><author>桔子汁</author><pubDate>Tue, 16 Sep 2008 06:34:00 GMT</pubDate><guid>http://www.blogjava.net/orangelizq/archive/2008/09/16/229158.html</guid><wfw:comment>http://www.blogjava.net/orangelizq/comments/229158.html</wfw:comment><comments>http://www.blogjava.net/orangelizq/archive/2008/09/16/229158.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/orangelizq/comments/commentRss/229158.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/orangelizq/services/trackbacks/229158.html</trackback:ping><description><![CDATA[<p>Java泛型编程指南</p>
<p>此系列文章译自SUN的泛型编程指南, 看不懂译文的请看原文<br />
http://java.sun.com/j2se/1.5/pdf/generics-tutorial.pdf</p>
<p>一、绪言<br />
JDK1.5对JAVA语言进行了做了几个扩展，其中一个就是泛型。<br />
本指南旨在介绍泛型。如果你熟悉其它语言的构造类似的东西，特别是C++的模<br />
板（template），你会很快发现它们之间的相同点及重要的不同点；如果你在其他<br />
地方没看到过类似的东西，那反而更好，那样你就可以开始全新的学习，用不着去忘<br />
掉那些（对JAVA泛型）容易产生误解的东西。<br />
泛型允许你对类型进行抽象。最常见的例子是容器类型，比如那些在Collection<br />
层次下的类型。<br />
下面是那类例子的典型用法：</p>
<p>List myIntList = new LinkedList();//1<br />
myIntList.add(new Integer(0));//2<br />
Integer x = (Integer) myIntList.iterator().next();//3</p>
<p>第3行里的强制类型转换有点烦人，程序通常都知道一个特定的链表（list）里<br />
存放的是何种类型的数据，但却一定要进行类型转换。编译器只能保证迭代器返回的<br />
是一个对象，要保证对Integer类型变量的赋值是类型安全的话，必须进行类型转换。<br />
类型转换不但会引起程序的混乱，还可能会导致运行时错误，因为程序员可能会<br />
犯错误。<br />
如果程序员可以如实地表达他们的意图，即标记一个只能包含特定数据类型的链<br />
表，那会怎么样呢？这就是泛型背后的核心思想。下面是前面代码的泛型写法：<br />
List&lt;Integer&gt; myIntList = new LinkedList&lt;Integer&gt;();//1'<br />
myIntList.add(new Integer(0));//2'<br />
Integer x = myIntList.iterator().next();//3'</p>
<p>请注意变量myIntList的类型声明，它指明了这不仅仅是一个任意的List，还<br />
是一个Integer类型的List，写作List&lt;Integer&gt;。我们说List是一个接受类型（在<br />
这个例子是Integer）参数的泛华的接口，在创建链表对象的时候，我们也指定了一个<br />
类型参数。<br />
另外要注意的是在第3'行的类型转换已经不见了。<br />
现在你可能会想，我们所做的全部都是为了把混乱消除。我们没有在第3行把类<br />
型转换为Integer，而是在第1'行加了Integer类型参数；非也非也，这里面差别很<br />
大，编译器现在能够在编译期间检测程序的类型正确性。当我们把myIntList声明为<br />
类型List&lt;Integer&gt;的后，就意味着变量myIntList在何时何地的使用都是正确的，<br />
编译器保证了这一点。相反，类型转换只是告诉我们程序员认为它在程序的某个地方<br />
是正确的。<br />
实际的结果是，程序（特别是大型的程序）的可读性和健壮性得到了提高。<br />
免费linux公开课,，现在报名!<br />
&nbsp;<br />
&nbsp;</p>
<p>&nbsp;&nbsp; 二、定义简单的泛型</p>
<p>下面是java.util包里的List和Iterator接口定义的一个小小的引用：</p>
<p>public interface List&lt;E&gt;{<br />
void add(E x);<br />
Iterator&lt;E&gt; iterator();<br />
}<br />
public interface Iterator&lt;E&gt;{<br />
E next();<br />
boolean hasNext();<br />
}</p>
<p>除了尖括号里的东西，这里所有的都应该很熟悉了。那是List和Iterator接口<br />
的规范类型参数的声明。<br />
类型参数可以用在任何的泛型声明中，就像使用普通的类型一样（虽然有一些很<br />
重要的限制；看第7部分）。<br />
在绪言中，我们看到了List泛型声明的调用，比如List&lt;Integer&gt;。在调用里面<br />
（通常称为参数化类型），所有出现规范类型参数（这里是E）的全部都用实际的类型<br />
参数（这里是Integer）所代替。<br />
你可以想象成List&lt;Integer&gt;代表所有E都用Integer代替了的List：</p>
<p>public interface IntegerList{<br />
void add(Integer x)<br />
Iterator&lt;Integer&gt; iterator();<br />
}</p>
<p>这种想法是有所帮助的，但也会造成误解。<br />
它是有所帮助的，是因为参数化类型List&lt;integer&gt;有看起来像这种扩展的方法。<br />
它会造成误解，是因为泛型的声明实际上不会像那样去扩展；在源代码中、二进制<br />
文件中、硬盘和内在里，都没有代码的多个拷贝。如果你是一个C++程序员，你会明白<br />
这跟C++的模板（template）很不同。<br />
泛型声明是一次编译，永远使用，它会变成一个单独的class文件，就像一个普通<br />
的类或接口声明。<br />
类型参数跟用在方法或构造函数里的普通的参数类似，就像一个方法具有描述它运<br />
算用到的值的类型的规范值参一样，泛化声明具有规范类型参数。当一个方法被调用的<br />
时候，实际的参数将会被规范参数所代替而对方法求值。当一个泛化声明被调用的时候，<br />
实际类型参数将会代替规范类型参数。<br />
命名惯例要注意的一个地方。我们建议你用一些简炼（如果可以的话只用一个字<br />
符）但却映眼的名字作为规范类型参数名。在那些名字中最后避免小写字母，这样可<br />
以很容易把规范类型参数和普通的类或接口区分开来。就像前面的例子一样，很多容<br />
器类型使用E。我们将会在后面的例子里看到其他的惯例。<br />
免费linux公开课,，现在报名!<br />
&nbsp;<br />
&nbsp;</p>
<p>&nbsp;&nbsp; 三、泛型和子类化</p>
<p>http://xoj.blogone.net</p>
<p>我们来测试一下对泛型的理解，下面的代码是否正确呢？</p>
<p>List&lt;String&gt; ls = new ArrayList&lt;String&gt;();//1<br />
List&lt;Object&gt; lo = ls;//2</p>
<p>第1行肯定是正确的，问题的难点在于第2行；这样就归结为这个问题：一个字符<br />
串（String）链表（List）是不是一个对象链表？大部分人的直觉是：&#8220;肯定了！&#8221;<br />
那好，看一下下面这两行：</p>
<p>lo.add(new Object());//3<br />
String s = ls.get(0);//4:企图把一个对象赋值给字符串！</p>
<p>在这里我们把ls和lo搞混淆了。我们通过别名lo来访问字符串链表ls，插入不<br />
确定对象；结果就是ls不再存储字符串，当我们尝试从里面取出数据的时候就会出错。<br />
Java编译器当然不允许这样的事情发生了，所以第2行肯定会编译出错。<br />
一般来说，如果Foo是Bar的子类型（子类或子接口），而G又是某个泛型声明的<br />
话，G&lt;Foo&gt;并不是G&lt;Bar&gt;的子类型。这可能是学习泛型的时候最难的地方，因为它<br />
与我们的深层直觉相违背。<br />
直觉出错的问题在于它把集合里的东西假想为不会改变的，我们的本能把这些东<br />
西看作是不变的。<br />
举个例子，假设汽车公司为人口调查局提供一份驾驶员的列表，这看上去挺合理。<br />
假设Driver是Person的一个子类，则我们认为List&lt;Driver&gt;是一个List&lt;Person&gt;。<br />
而实际上提交的是一份驾驶员登记表的一个副本。否则的话，人口调查局将可以驾驶员<br />
的人加入到那份列表中去，汽车公司的纪录受到破坏。<br />
为了解决这类问题，我们需要考虑一些更灵活的泛型，到现在为止碰到的规则太<br />
受约束了。<br />
免费linux公开课,，现在报名!<br />
&nbsp;<br />
&nbsp;</p>
<p>&nbsp;&nbsp; 四、通配符</p>
<p>http://xoj.blogone.net</p>
<p>考虑一下写一个程序来打印一个集合对象（collection）里的所有元素。<br />
在旧版的语言里面，你可以会像下面那样写：</p>
<p>void printCollection(Collection c){<br />
Iterator i = c.iterator();<br />
for (k = 0; k &lt; c.size(); k++){<br />
System.out.println(i.next());<br />
}<br />
}</p>
<p>下面尝试着用泛型（和新的for循环语法）来写：</p>
<p>void printCollection(Collection&lt;Object&gt; c){<br />
for (Object e : c) {<br />
System.out.println(e);<br />
}<br />
}</p>
<p>这样的问题是新版本的代码还没旧版本的代码好用。就像我们刚示范的一样，<br />
Collection&lt;Object&gt;并不是所有类型的集合的父类型，所以它只能接受Collection&lt;Object&gt;<br />
对象，而旧版的代码却可以把任何类型的集合对象作为参数来调用。<br />
那么，什么才是所有集合类型的父类型呢？这个东西写作Collection&lt;?&gt;（读<br />
作&#8220;未知集合&#8221;），就是元素类型可以为任何类型的集合。这就是它为什么被称为&#8220;通<br />
配符类型&#8221;的原因。我们可以这样写：</p>
<p>void printCollection(Collection&lt;?&gt; c){<br />
for (Object e : c) {<br />
System.out.println(e);<br />
}<br />
}</p>
<p>现在，我们就可以以任何类型的集合对象作为参数来调用了。注意，在printCollection()<br />
方法里面，我们仍然可以从c对象中读取元素并赋予Object类型；因为无论集合里<br />
实际包含了什么类型，它肯定是对象，所以是类型安全的。但对它插入任意的对象<br />
的话则是不安全的：</p>
<p>Collection&lt;?&gt; c = new ArrayList&lt;String&gt;();<br />
c.add(new Object());//编译错误</p>
<p>由于我们并不知道c的元素类型是什么，因此我们不能对其插入对象。add()方法<br />
接受类型E，即集合的元素类型的参数。当实际的类型参数是?的时候，就代表是某未<br />
知类型。任何传递给add方法的参数，其类型必须是该未知类型的子类型。因为我们并<br />
不知道那是什么类型，所以我们传递不了任何参数。唯一的例外就是null，因为它是任<br />
何（对象）类型的成员。<br />
另外，假设有一个List&lt;?&gt;，我们可以调用get()方法并使用其返回结果。结果<br />
类型是一个未知类型，但我们都知道它是一个对象。因此把get()方法的返回结果赋<br />
值给对象类型，或者把它作为一个对象参数传递都是类型安全的。<br />
免费linux公开课,，现在报名!<br />
&nbsp;<br />
&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </p>
<p>&nbsp;&nbsp; 四、1-有界通配符</p>
<p>http://xoj.blogone.net</p>
<p>考虑一个简单的画图程序，它可以画长方形和圆等形状。为了表示这些形状，<br />
你可能会定义这样的一个类层次结构：</p>
<p>public abstract class Shape{<br />
public abstract void draw(Canvas c);<br />
}<br />
public class Circle extends Shape{<br />
private int x, y, radius;<br />
public void draw(Canvas c) { ... }<br />
public class Rectangle extends Shape {<br />
private int x, y, width, height;<br />
public void draw(Canvas c) { ... }<br />
}</p>
<p>这些类可以在canvas上描画：</p>
<p>public class Canvas {<br />
public void draw(Shape s) {<br />
s.draw(this);<br />
}<br />
}</p>
<p>任何的描画通常都包括有几种形状，假设它们用一个链表来表示，那么如果在<br />
Canvas里面有一个方法来画出所有的形状的话，那将会很方便：</p>
<p>public void drawAll(List&lt;Shape&gt; shapes) {<br />
for (Shape s: shapes) {<br />
s.draw(this);<br />
}<br />
}</p>
<p>但是现在，类型的规则说drawAll()方法只能对确切的Shape类型链表调用，<br />
比如，它不能对List&lt;Circle&gt;类型调用该方法。那真是不幸，因为这个方法所要<br />
做的就是从链表中读取形状对象，从而对List&lt;Circle&gt;类型对象进行调用。我们<br />
真正所想的是要让这个方法能够接受一个任何形状的类型链表：</p>
<p>public void drawAll(List&lt;? extends Shape&gt; shapes) { ... }</p>
<p>这里有一个很小但很重要的不同点：我们把类型List&lt;Shape&gt;替换为List&lt;? extends Shape&gt;。<br />
现在drawAll()方法可以接受任何Shape子类的链表，我们就可以如愿的对List&lt;Circle&gt;<br />
调用进行啦。<br />
List&lt;? extends Shape&gt;是一个有界通配符的例子。? 表示一个未知类型，<br />
就像我们之前所看到的通配符一样。但是，我们知道在这个例子里面这个未知类型<br />
实际是Shape的子类型（注：它可以是Shape本身，或者是它的子类，无须在字面上<br />
表明它是继承Shape类的）。我们说Shape是通配符的&#8220;上界&#8221;。<br />
如往常一样，使用通配符带来的灵活性得要付出一定的代价；代码就是现在在<br />
方法里面不能对Shape对象插入元素。例如，下面的写法是不允许的：</p>
<p>public void addRectangle(List&lt;? extends Shape&gt; shapes) {<br />
shapes.add(0, new Rectangle()); //编译错误<br />
}</p>
<p>你应该可以指出为什么上面的代码是不允许的。shapes.add()方法的第二个<br />
参数的类型是 ? 继承Shape，也就是一个未知的Shape的子类型。既然我们不知道<br />
类型是什么，那么我们就不知道它是否是Rectangle的父类型了；它可能是也可能<br />
不是一个父类型，因此在那里传递一个Rectangle的对象是不安全的。<br />
有界通配符正是需要用来处理汽车公司给人口调查局提交数据的例子方法。在<br />
我们的例子里面，我们假设数据表示为姓名（用字符串表示）对人（表示为引用类<br />
型，比如Person或它的子类型Driver等）的映射。Map&lt;K, V&gt;是有两个类型参数的<br />
一个泛型的例子，表示键值映射。<br />
请再一次注意规范类型参数的命名惯例：K表示键，V表示值。</p>
<p>public class Census {<br />
public static void<br />
addRegistry(Map&lt;String, ? extends Person&gt; registry){ ... }<br />
}<br />
...<br />
Map&lt;String, Driver&gt; allDrivers = ...;<br />
Census.addRegistry(allDrivers);<br />
免费linux公开课,，现在报名!<br />
&nbsp;<br />
&nbsp;&nbsp; 五、泛型方法</p>
<p>http://xoj.blogone.net</p>
<p>考虑写这样一个方法，它接收一个数组和一个集合（collection）作为参数，<br />
并把数组里的所有对象放到集合里面。<br />
先试试这样：</p>
<p>static void fromArrayToCollection(Object[] a, Collection&lt;?&gt; c){<br />
for (Object o : a){<br />
c.add(o);//编译错误<br />
}<br />
}</p>
<p>到现在，你应该学会了避免把Collection&lt;Object&gt;作为集合参数的类型这种初学<br />
者的错误；你可能或可能没看出使用Collection&lt;?&gt;也是不行的，回想一下，你是不能<br />
把对象硬塞进一个未知类型的集合里面的。<br />
解决这类问题的方法是使用泛型方法。就像类型声明一样，方法也可以声明为泛型<br />
的，就是说，用一个或多个类型参数作为参数。</p>
<p>static &lt;T&gt; void fromArrayToCollection(T[]a, Collection&lt;T&gt; c){<br />
for (T o : a){<br />
c.add(o);//正确<br />
}<br />
}</p>
<p>对于集合元素的类型是数组类型的父类型，我们就可以调用这个方法。</p>
<p>Object[] oa = new Object[100];<br />
Collection&lt;Object&gt; co = new ArrayList&lt;Object&gt;();<br />
fromArrayToCollection(oa, co);// T是对象类型<br />
String[] sa = new String[100];<br />
Collection&lt;String&gt; cs = new ArrayList&lt;String&gt;();<br />
fromArrayToCollection(sa, cs);// T是字符串类型（String）<br />
fromArrayToCollection(sa, co);// T对象类型<br />
Integer[] ia = new Integer[100];<br />
Float[] fa = new Float[100];<br />
Number[] na = new Number[100];<br />
Collection&lt;Number&gt; cn = new ArrayList&lt;Number&gt;();<br />
fromArrayToCollection(ia, cn);// T是Number类型<br />
fromArrayToCollection(fa, cn);// T是Number类型<br />
fromArrayToCollection(na, cn);// T是Number类型<br />
fromArrayToCollection(na, co);// T是Number类型<br />
fromArrayToCollection(na, cs);// 编译错误</p>
<p>请注意，我们并没有把实际的类型实参传递给泛型方法，因为编译器会根据<br />
实参的类型为我们推断出类型实参。一般地，编译器推断得到可以正确调用的最<br />
接近的（the most specific）实参类型。<br />
现在有一个问题：我应该什么时候使用泛型方法，什么时候使用通配符类型<br />
呢？为了明白这个问题的答案，我们来看看Collection库里的几个方法：</p>
<p>interface Collection&lt;E&gt;{<br />
public boolean containsAll(Collection&lt;?&gt; c);<br />
public boolean addAll(Collection&lt;? extends E&gt; c);<br />
}</p>
<p>在这里我们也可以用泛型方法：</p>
<p>interface Collection&lt;E&gt;{<br />
public &lt;T&gt; boolean containsAll(Collection&lt;T&gt; c);<br />
public &lt;? extends E&gt;boolean addAll(Collection&lt;T&gt; c);<br />
//哈哈，类型变量也可以有界！<br />
}</p>
<p>但是，类型参数T在containsAll和addAll两个方法里面都只是用了一次。返<br />
回类型并不依赖于类型参数或其他传递给该方法的实参（这种是只有一个实参的简单<br />
情况）。这就告诉我们类型实参是用于多态的，它的作用只是对不同的调用可以有一<br />
系列的实际的实参类型。如果是那样的话，就应该使用通配符，通配符就是设计来支<br />
持灵活的子类型的，这也是我们这里所要表述的东西。<br />
泛型方法允许类型参数用于表述一个或多个的实参类型对方法或及其返回类型的<br />
依赖关系。如果没有那样的一个依赖关系的话，泛型方法就不应用使用。<br />
也有可能是一前一后一起使用泛型方法和通配符的情况，下面是Collections.copy()<br />
方法：</p>
<p>class Collections {<br />
public static &lt;T&gt; void copy(List&lt;T&gt; dest, list&lt; ? extends T&gt; src) {...}<br />
}</p>
<p>请注意这里两个参数类型的依赖关系，任何要从源链表src复制过来的对象都必<br />
须是对目标链表dst元素可赋值的；所以我们可以不管src的元素类型是什么，只要<br />
它是T类型的子类型。copy方法的方法头表示了使用一个类型参数，但是用通配符来<br />
作为第二个参数的元素类型的依赖关系。<br />
我们是可以用另外一种不用通配符来写这个方法头的办法。</p>
<p>class Collections {<br />
public static &lt;T, S extends T&gt;<br />
vod copy(List&lt;T&gt; dest, List&lt;S&gt; src) { ...}<br />
}</p>
<p>没问题，但是当第一个类型参数用作dst的类型和批二个类型参数S的上界的<br />
时候，S它本身在src类型里只能使用一次，没有其他的东西依赖于它。这就意味<br />
着我们可以用一个通配符来代替S了。使用通配符比声明显式的类型参数要来得清<br />
晰和简单，因此在可能的话都优先使用通配符。<br />
当通配符用于方法头外部，作为成员变量、局部变量和数组的类型的时候，同<br />
样也有优势。请看下面的例子。<br />
看回我们之前画图的那个问题，现在我们想要保留一份画图请求的历史记录。<br />
我们可以这样来维护这份历史记录，在Shape类里用一个静态的变量表示历史记录，<br />
然后在drawAll()方法里面把传递的实参储存到那历史记录变量里头。</p>
<p>static List&lt;List&lt;? extends Shape&gt;&gt; history = <br />
new ArrayList&lt;List&lt;? extends Shape&gt;&gt;();<br />
public void drawAll(List&lt;? extends Shape&gt; shapes){<br />
history.addLast(shapes);<br />
for (Shape s: shapes) {<br />
s.draw(this);<br />
}<br />
}</p>
<p>最后，我们再次留意一下使用类型参数的命名惯例。当没有更精确的类型来<br />
区分的时候，我们用T来表示类型，这是通常是在泛型方法里面的情况。如果有多<br />
个类型参数，我们可以用在字母表中与T相邻的字母来表示，比如S。如果一个泛<br />
型方法出现在一个泛型类里面，一个好的方法就是，应该避免对方法和类使用相<br />
同的类型参数以免发生混淆。这在嵌套泛型类里也一样。<br />
免费linux公开课,，现在报名!<br />
&nbsp;<br />
&nbsp;</p>
<p>&nbsp;&nbsp; 六、与遗留代码的交互</p>
<p><br />
到现在为止，我们所有的例子都是在一个假想的理想世界里面的，就是所有的<br />
人都在使用Java语言支持泛型的最新版本。<br />
唉，不过在现实中情况却不是那样。千百万行的代码都是用早期版本的语言<br />
来编写的，不可能把它们全部在一夜之间就转换过来。<br />
在后面的第10部分，我们将会解决把遗留代码转为用泛型这个问题。在这部分<br />
我们要看的是比较简单的问题：遗留代码与泛型代码如何交互？这个问题分为两个<br />
部分：在泛型代码中使用遗留代码和在遗留代码中使用泛型代码。<br />
免费linux公开课,，现在报名!<br />
&nbsp;</p>
<p><br />
&nbsp;&nbsp; 六-1 在泛型代码中使用遗留代码</p>
<p>[url=http://xoj.blogone.net][url]<br />
当你在享受在代码中使用泛型带来的好处的时候，你怎么样使用遗留代码呢？<br />
假设这样一个例子，你要使用com.Foodlibar.widgets这个包。Fooblibar.com<br />
的人要销售一个库存控制系统，主要部分如下：</p>
<p>package com.Fooblibar.widgets;<br />
public interface Part { ... }<br />
public class Inventory {<br />
/**<br />
*Adds a new Assembly to the inventory databse.<br />
*The assembly is given the name name, and consists of a set<br />
*parts specified by parts. All elements of the collection parts<br />
*must support the Part interface.<br />
**/<br />
public static void addAssembly(String name, Collection parts) {...}<br />
public static Assembly getAssembly(String name) {...}<br />
}<br />
public interface Assembly{<br />
Collection getParts();//Returns a collection of Parts<br />
}</p>
<p>现在，你可以用上面的API来增加新的代码，它可以很好的保证你调用参数恰当<br />
的addAssembly()方法，就是说传递的集合是一个Part类型的Collection对象，当<br />
然，泛型是最适合做这个：</p>
<p>package com.mycompany.inventory;<br />
import com.Fooblibar.widgets.*;<br />
public class Blade implements Part{<br />
...<br />
}<br />
public class Guillotine implements Part {<br />
}<br />
public class Main {<br />
public static void main(Sring[] args) {<br />
Collection&lt;Part&gt; c = new ArrayList&lt;Part&gt;();<br />
c.add(new Guillotine());<br />
c.add(new Blade());<br />
Inventory.addAssembly("thingee", c);<br />
Collection&lt;Part&gt; k = Inventory.getAssembly("thingee").getParts();<br />
}<br />
}</p>
<p>当我们调用addAssembly方法的时候，它想要的第二个参数是Collection类型的，<br />
实参是Collection&lt;Part&gt;类型，但却可以，为什么呢？毕竟，大多数集合存储的都不是<br />
Part对象，所以总的来说，编译器不会知道Collection存储的是什么类型的集合。<br />
在正规的泛型代码里面，Collection都带有类型参数。当一个像Collection这样<br />
的泛型不带类型参数使用的时候，称之为原生类型。<br />
很多人的第一直觉是Collection就是指Collection&lt;Object&gt;，但从我们先前所<br />
看到的可以知道，当需要的对象是Collection&lt;Object&gt;，而传递的却是Collection&lt;Part&gt;<br />
对象的时候，是类型不安全的。确切点的说法是Collection类型表示一个未知类型的<br />
集合，就像Collection&lt;?&gt;。<br />
稍等一下，那样做也是不正确的！考虑一下调用getParts()方法，它返回一个<br />
Collection对象，然后赋值给k，而k是Collection&lt;Part&gt;类型的；如果调用的结果<br />
是返回一个Collection&lt;?&gt;的对象，这个赋值可能是错误的。<br />
事实上，这个赋值是允许的，只是它会产生一个未检测警告。警告是需要的，因为<br />
编译器不能保证赋值的正确性。我们没有办法通过检测遗留代码中的getAssembly()方法<br />
来保证返回的集合的确是一个类型参数是Part的集合。程序里面的类型是Collection，<br />
我们可以合法的对此集合插入任何对象。<br />
所以，这不应该是错误的吗？理论上来说，答案是：是；但实际上如果是泛型代码<br />
调用遗留代码的话，这又是允许的。对这个赋值是否可接受，得取决于程序员自己，在<br />
这个例子中赋值是安全的，因为getAssembly()方法约定是返回以Part作为类型参数的<br />
集合，尽管在类型标记中没有表明。<br />
所以原生类型很像通配符类型，但它们没有那么严格的类型检测。这是有意设计成<br />
这样的，从而可以允许泛型代码可以与之前已有的遗留代码交互。<br />
在泛型代码中调用遗留代码固然是危险的，一旦把泛型代码和非泛型代码混合在一<br />
起，泛型系统所提供的全部安全保证就都变得无效了。但这仍比根本不使用泛型要好，<br />
最起码你知道你的代码是一致的。<br />
泛型代码出现的今天，仍然有很多非泛型代码，二者混合同时使用是不可避免的。<br />
如果一定要把遗留代码与泛型代码混合使用，请小心留意那些未检测警告。仔细的<br />
想想如何才能判定引发警告的代码是安全的。<br />
如果仍然出错，代码引发的警告实际不是类型安全的，那又怎么样呢？我们会看<br />
那样的情况，接下来，我们将会部分的观察编译器的工作方式。<br />
免费linux公开课,，现在报名!<br />
&nbsp;<br />
&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </p>
<p>&nbsp;&nbsp; 六-2 擦除和翻译</p>
<p><br />
public String loophole(Integer x){<br />
List&lt;String&gt; ys = new LinkedList&lt;String&gt;();<br />
List xs = ys;<br />
xs.add(x);//编译时未检测警告<br />
return ys.iterator().next();<br />
}</p>
<p>在这里我们定义了一个字符串类型的链表和一个一般的老式链表，我们先插入<br />
一个Integer对象，然后试图取出一个String对象，很明显这是错误的。如果我们<br />
忽略警告继续执行代码的话，程序将会在我们使用错误类型的地方出错。在运行时，<br />
代码执行大致如下：</p>
<p>public String loophole(Integer x) {<br />
List ys = new LinkedList;<br />
List xs = ys;<br />
xs.add(x);<br />
return (String)ys.iterator().next();//运行时出错<br />
}</p>
<p>当我们要从链表中取出一个元素，并把它当作是一个字符串对象而把它转换为<br />
String类型的时候，我们将会得到一个ClassCastException类型转换异常。在<br />
泛型版本的loophole()方法里面发生的就是这种情况。<br />
出现这种情况的原因是，Java的泛型是通过一个前台转换&#8220;擦除&#8221;的编译器实现<br />
的，你基本上可以认为它是一个源码对源码的翻译，这就是为何泛型版的loophole()<br />
方法转变为非泛型版本的原因。<br />
结果是，Java虚拟机的类型安全性和完整性永远不会有问题，就算出现未检测<br />
的警告。<br />
基本上，擦除会除去所有的泛型信息。尖括号里面的所有类型信息都会去掉，比<br />
如，参数化类型的List&lt;String&gt;会转换为List。类型变量在之后使用时会被类型<br />
变量的上界（通常是Object）所替换。当最后代码不是类型正确的时候，就会加入<br />
一个适当的类型转换，就像loophole()方法的最后一行。<br />
对&#8220;擦除&#8221;的完整描述不是本指南的范围内的内容，但前面我们所给的简单描述<br />
也差不多是那样了。了解这点很有好处，特别是当你想做诸如把现有API转为使用<br />
泛型（请看第10部分）这样复杂的东西，或者是想知道为什么它们会那样的时候。<br />
免费linux公开课,，现在报名!<br />
&nbsp;<br />
&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br />
&nbsp;&nbsp; 六-3 在遗留代码中使用泛型</p>
<p>&nbsp;</p>
<p>现在我们来看看相反的情况。假设Fooblibar.com把他们的API转换为泛型的，<br />
但有些客户还没有转换。代码就会像下面的：</p>
<p>package com.Fooblibar.widgets;<br />
public interface Part { ... }<br />
publlic class Inventory {<br />
/**<br />
*Adds a new Assembly to the inventory database.<br />
*The assembly is given the name name, and consists of a set<br />
*parts specified by parts. All elements of the collection parts<br />
*must support the Part interface.<br />
**/<br />
public static void addAssembly(String name, Collection&lt;Part&gt; parts) {...}<br />
public static Assembly getAssembly(String name){ ... }<br />
}<br />
public interface Assembly {<br />
Collection&lt;Part&gt; getParts();//Return a collection of Parts<br />
}</p>
<p>客户代码如下：</p>
<p>package com.mycompany.inventory;<br />
import com.Fooblibar.widgets.*;<br />
public class Blade implements Part {<br />
...<br />
}<br />
public class Guillotine implements Part {<br />
...<br />
}<br />
public class Main {<br />
public static void main(String[] args){<br />
Collection c = new ArrayList();<br />
c.add(new Guillotine());<br />
c.add(new Blade());<br />
Inventory.addAssembly("thingee", c);//1: unchecked warning<br />
Collection k = Inventory.getAssembly("thingee").getParts();<br />
}<br />
}</p>
<p>客户代码是在引进泛型之前写下的，但是它使用了com.Fooblibar.widgets包和集<br />
合库，两个现在都是在用泛型的。在客户代码里面使用的泛型全部都是原生类型。<br />
第1行产生一个未检测警告，因为把一个原生Collection传递给了一个需要Part类型的<br />
Collection的地方，编译器不能保证原生的Collection是一个Part类型的Collection。<br />
不这样做的话，你也可以在编译客户代码的时候使用source 1.4这个标记来保证不<br />
会产生警告。但是这样的话你就不能使用所有JDK 1.5引入的新的语言特性。<br />
免费linux公开课,，现在报名!<br />
&nbsp;<br />
&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </p>
<p><br />
&nbsp;&nbsp; 七、晦涩难懂的部分</p>
<p>七-1 泛型类为所有调用所共享<br />
下面的代码段会打印出什么呢？</p>
<p>List&lt;String&gt; l1 = new ArrayList&lt;String&gt;();<br />
List&lt;Integer&gt; l2 = new ArrayList&lt;Integer&gt;();<br />
System.out.println(l1.getClass() == l2.getClass());</p>
<p>你可能会说是false，但是你错了，打印的是true，因为所有泛型类的实例它们<br />
的运行时的类（run-time class）都是一样的，不管它们实际类型参数如何。<br />
泛型类之所以为泛型的，是因为它对所有可能的类型参数都有相同的行为，相同<br />
的类可以看作是有很多不同的类型。<br />
结果就是，一个类的静态的变量和方法也共享于所有的实例中，这就是为什么不<br />
允许在静态方法或初始化部分、或者在静态变量的声明或初始化中引用类型参数。<br />
免费linux公开课,，现在报名!<br />
&nbsp;<br />
&nbsp;<br />
&nbsp;<br />
&nbsp;</p>
<p>&nbsp;&nbsp; 七-2 强制类型转换和instanceof</p>
<p>泛型类在它所有的实例****享，就意味着判断一个实例是否是一个特别调用的泛<br />
型的实例是毫无意义的：</p>
<p>Collection cs = new ArrayList&lt;String&gt;();<br />
if (cs instanceof Collection&lt;String&gt;) {...}//非法</p>
<p>类似地，像这样的强制类型转换：</p>
<p>Collection&lt;String&gt; cstr = (Collection&lt;String&gt;) cs;//未检测警告</p>
<p>给出了一个未检测的警告，因为这里系统在运行时并不会检测。<br />
对于类型变量也一样：</p>
<p>&lt;T&gt; T BadCast(T t, Object o) {<br />
return (T) o;//未检测警告<br />
}</p>
<p>类型变量不存在于运行时，这就是说它们对时间或空间的性能不会造成影响。<br />
但也因此而不能通过强制类型转换可靠地使用它们了。<br />
免费linux公开课,，现在报名!<br />
&nbsp;<br />
&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br />
&nbsp;&nbsp; 七-3 数组</p>
<p>数组对象的组件类型可能不是一个类型变量或一个参数化类型，除非它是一个<br />
（无界的）通配符类型。你可以声明元素类型是类型变量和参数华类型的数组类型，<br />
但元素类型不能是数组对象。<br />
这自然有点郁闷，但这个限制对避免下面的情况是必要的：</p>
<p>List&lt;Strign&gt;[] lsa = new List&lt;String&gt;[10];//实际上是不允许的<br />
Object o = lsa;<br />
Object[] oa = (Object[]) o;<br />
List&lt;Integer&gt; li = new ArrayList&lt;Integer&gt;();<br />
li.add(new Integer(8));<br />
oa[1] = li;//不合理，但可以通过运行时的赋值检测<br />
String s = lsa[1].get(0);//运行时出错：ClassCastException异常</p>
<p>如果参数化类型的数组允许的话，那么上面的例子编译时就不会有未检测的警告，<br />
但在运行时出错。对于泛型编程，我们的主要设计目标是类型安全，而特别的是这个<br />
语言的设计保证了如果使用了javac -source 1.5来编译整个程序而没有未检测的<br />
警告的话，它是类型安全的。<br />
但是你仍然会使用通配符数组，这与上面的代码相比有两个变化。首先是不使用<br />
数组对象或元素类型被参数化的数组类型，这样我们就需要在从数组中取出一个字符<br />
串的时候进行强制类型转换：</p>
<p>List&lt;?&gt;[] lsa = new List&lt;?&gt;[10];//没问题，无界通配符类型数组<br />
Object o = lsa;<br />
Object[] oa = (Object[]) o;<br />
List&lt;Integer&gt; li = new ArrayList&lt;Integer&gt;();<br />
li.add(new Integer(3));<br />
oa[1] = li;//正确<br />
String s = (String) lsa[1].get(0);//运行时错误，显式强制类型转换</p>
<p>第二个变化是，我们不创建元素类型被参数化的数组对象，但仍然使用参数化元素<br />
类型的数组类型，这是允许的，但引起现未检测警告。这样的程序实际上是不安全的，<br />
甚至最终会出错。</p>
<p>List&lt;String&gt;[] lsa = new List&lt;?&gt;[10];//未检测警告-这是不安全的！<br />
Object o = lsa;<br />
Object[] oa = (Object[]) o;<br />
List&lt;Integer&gt; li = new ArrayList&lt;integer&gt;();<br />
li.add(new Integer(3));<br />
oa[1]=li;//正确<br />
String s = lsa[1].get(0);//运行出错，但之前已经被警告</p>
<p>类似地，想创建一个元素类型是类型变量的数组对象的话，将会编译出错。</p>
<p>&lt;T&gt; T[] makeArray(T t){<br />
return new T[100];//错误<br />
}</p>
<p>因为类型变量并不存在于运行时，所以没有办法知道实际的数组类型是什么。<br />
要突破这类限制，我们可以用第8部分说到的用类名作为运行时标记的方法。<br />
免费linux公开课,，现在报名!<br />
&nbsp;<br />
&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br />
&nbsp;&nbsp; 八、 把类名作为运行时的类型标记</p>
<p>JDK1.5中的一个变化是java.lang.Class是泛化的，一个有趣的例子是对<br />
容器外的东西使用泛型。<br />
现在Class类有一个类型参数T，你可能会问，T代表什么啊？它就代表Class<br />
对象所表示的类型。<br />
比如，String.class的类型是Class&lt;String&gt;，Serializable.class的<br />
类型是Class&lt;Serializable&gt;，这可以提高你的反射代码中的类型安全性。<br />
特别地，由于现在Class类中的newInstance()方法返回一个T对象，因此<br />
在通过反射创建对象的时候可以得到更精确的类型。<br />
其中一个方法就是显式传入一个factory对象，代码如下：</p>
<p>interface Factory&lt;T&gt; {T make();}<br />
public &lt;T&gt; Collection&lt;T&gt; select(Factory&lt;T&gt; factory, String statement){<br />
Collection&lt;T&gt; result = new ArrayList&lt;T&gt;();<br />
//用JDBC运行SQL查询<br />
for(/*遍历JDBC结果*/){<br />
T item = factory.make();<br />
/*通过SQL结果用反射和设置数据项*/<br />
result.add(item);<br />
}<br />
return result;<br />
}</p>
<p>你可以这样调用：</p>
<p>select(new Factory&lt;EmpInfo&gt;(){ public EmpInfo make() {<br />
return new EmpInfo();<br />
}}<br />
, "selection string");</p>
<p>或者声明一个EmpInfoFactory类来支持Factory接口：</p>
<p>class EmpInfoFactory implements Factory&lt;EmpInfo&gt;{<br />
...<br />
public EmpInfo make() { return new EmpInfo();}<br />
}</p>
<p>然后这样调用：</p>
<p>select(getMyEmpInfoFactory(), "selection string");</p>
<p>这种解决办法需要下面的其中之一：<br />
&#183; 在调用的地方使用详细的匿名工厂类(verbose anonymous factory classes)，或者<br />
&#183; 为每个使用的类型声明一个工厂类，并把工厂实例传递给调用的地方，这样有点不自然。</p>
<p>使用类名作为一个工厂对象是非常自然的事，这样的话还可以为反射所用。现在<br />
没有泛型的代码可能写作如下：</p>
<p>Collection emps = sqlUtility.select(EmpInfo.class, "select * from emps");<br />
...<br />
public static Collection select(Class c, String sqlStatement) {<br />
Collection result = new ArrayList();<br />
/*用JDBC执行SQL查询*/<br />
for(/*遍历JDBC产生的结果*/){<br />
Object item = c.newInstance();<br />
/*通过SQL结果用反射和设置数据项*/<br />
result.add(item);<br />
}<br />
return result;<br />
}</p>
<p>但是，这样并不能得到我们所希望的更精确的集合类型，现在Class是泛化的，<br />
我们可以这样写：</p>
<p>Collection&lt;EmpInfo&gt; emps = <br />
sqlUtility.select(EmpInfo.class, "select * from emps");<br />
...<br />
public static &lt;T&gt; Collection&lt;T&gt; select(Class&lt;T&gt; c, String sqlStatement) {<br />
Collection&lt;T&gt; result = new ArrayList&lt;T&gt;();<br />
/*用JDBC执行SQL查询*/<br />
for(/*遍历JDBC产生的结果*/){<br />
T item = c.newInstance();<br />
/*通过SQL结果用反射和设置数据项*/<br />
result.add(item);<br />
}<br />
return result;<br />
}</p>
<p>这样就通过类型安全的方法来得到了精确的集合类型了。<br />
这种使用类名作为运行时类型标记的技术是一个很有用的技巧，是需要知道的。<br />
在处理注释的新的API中也有很多类似的情况。<br />
免费linux公开课,，现在报名!<br />
&nbsp;<br />
&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </p>
<p>&nbsp;&nbsp; 九 通配符的其他作用</p>
<p>（more fun with wildcards，不知道如何译才比较妥当，呵呵。）</p>
<p>在这部分，我们将会仔细看看通配符的几个较为深入的用途。我们已经从几个<br />
有界通配符的例子中看到，它对从某一数据结构中读取数据是很有用的。现在来看<br />
看相反的情况，只对数据结构进行写操作。<br />
下面的Sink接口就是这类情况的一个简单的例子：</p>
<p>interface Sink&lt;T&gt; {<br />
flush(T t);<br />
}</p>
<p>我们可以想象在下面的示范的例子中使用它，writeAll()方法用于把coll集合<br />
里的所有元素填充(flush)到Sink接口变量snk中，并返回最后一个填充的元素。</p>
<p>public static &lt;T&gt; T writeAll(Collection&lt;T&gt; coll, Sink&lt;T&gt; snk){<br />
T last;<br />
for (T t: coll){<br />
last = t;<br />
snk.flush(last);<br />
}<br />
return last;<br />
}<br />
...<br />
Sink&lt;Object&gt; s;<br />
Collection&lt;String&gt; cs;<br />
String str = writeAll(cs, s);//非法调用</p>
<p>如注释所注，这里对writeAll()方法的调用是非法的，因为无有效的类型参数<br />
可以引用；String和Object都不适合作为T的类型，因为Collection和Sink的元素<br />
必须是相同类型的。<br />
我们可以通过使用通配符来改写writeAll()的方法头来处理，如下：</p>
<p>public static &lt;T&gt; T writeAll(Collection&lt;? extends T&gt;, Sink&lt;T&gt;) {...}<br />
...<br />
String str = writeAll(cs, s);//调用没问题，但返回类型错误</p>
<p>现在调用是合法的了，但由于T的类型跟元素类型是Object的s一样，因为返回的<br />
类型也是Object，因此赋值是不正确的。<br />
解决办法是使用我们之前从未见过的一种有界通配符形式：带下界的通配符。<br />
语法 ? super T 表示了是未知的T的父类型，这与我们之前所使用的有界<br />
（父类型：或者T类型本身，要记住的是，你类型关系是自反的）<br />
通配符是对偶有界通配符，即用 ? extends T 表示未知的T的子类型。</p>
<p>public static&lt;T&gt; T writeAll(Collection&lt;T&gt; coll, Sink&lt;? super T&gt; snk) {...}<br />
...<br />
String str = writeAll(cs, s);//正确！</p>
<p>使用这个语法的调用是合法的，指向的类型是所期望的String类型。</p>
<p>现在我们来看一个比较现实一点的例子，java.util.TreeSet&lt;E&gt;表示元素类型<br />
是E的树形数据结构里的元素是有序的，创建一个TreeSet对象的一个方法是使用参数<br />
是Comparator对象的构造函数，Comparator对象用于对TreeSet对象里的元素进行<br />
所期望的排序进行分类。</p>
<p>TreeSet(Comparator&lt;E&gt; c)</p>
<p>Comparator接口是必要的：</p>
<p>interface Comparator&lt;T&gt; {<br />
int compare(T fst, T snd);<br />
}</p>
<p>假设我们想要创建一个TreeSet&lt;String&gt;对象，并传入一下合适的Comparator<br />
对象，我们传递的Comparator是能够比较字符串的。我们可以用Comparator&lt;String&gt;，<br />
但Comparator&lt;Object&gt;也是可以的。但是，我们不能对Comparator&lt;Object&gt;对象<br />
调用上面所给的构造函数，我们可以用一个下界通配符来得到我们想要的灵活性：</p>
<p>TreeSet(Comparator&lt;? super E&gt; c)</p>
<p>这样就可以使用适合的Comparator对象啦。<br />
最后一个下界通配符的例子，我们来看看Collections.max()方法，这个方法<br />
返回作为参数传递的Collection对象中最大的元素。<br />
现在，为了max()方法能正常运行，传递的Collection对象中的所有元素都必<br />
须是实现了Comparable接口的，还有就是，它们之间必须是可比较的。<br />
先试一下泛化方法头的写法：</p>
<p>public static &lt;T extends Comparable&lt;T&gt;&gt;<br />
T max(Collection&lt;T&gt; coll)</p>
<p>那样，方法就接受一个自身可比较的（comparable）某个T类型的Collection<br />
对象，并返回T类型的一个元素。这样显得太束缚了。<br />
来看看为什么，假设一个类型可以与合意的对象进行比较：</p>
<p>class Foo implements Comparable&lt;Object&gt; {...}<br />
...<br />
Collection&lt;Foo&gt; cf = ...;<br />
Collectins.max(cf);//应该可以正常运行</p>
<p>cf里的每个对象都可以和cf里的任意其他元素进行比较，因为每个元素都是Foo<br />
的对象，而Foo对象可以与任意的对象进行比较，特别是同是Foo对象的。但是，使用<br />
上面的方法头，我们会发现这样的调用是不被接受的，指向的类型必须是Foo，但Foo<br />
并没有实现Comparable&lt;Foo&gt;。<br />
T对于自身的可比性不是必须的，需要的是T与其父类型是可比的，就像下面：<br />
（实际的Collections.max()方法头在后面的第10部分将会讲得更多）</p>
<p>public static &lt;T extends Comparable&lt;? super T&gt;&gt;<br />
T max(Collection&lt;T&gt; coll)</p>
<p>这样推理出来的结果基本上适用于想用Comparable来用于任意类型的用法：<br />
就是你想这样用Comparable&lt;? super T&gt;。<br />
总的来说，如果你有一个只能一个T类型参数作为实参的API的话，你就应该用<br />
下界</p>
<img src ="http://www.blogjava.net/orangelizq/aggbug/229158.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/orangelizq/" target="_blank">桔子汁</a> 2008-09-16 14:34 <a href="http://www.blogjava.net/orangelizq/archive/2008/09/16/229158.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>JAVA中多种计时器的比较与分析</title><link>http://www.blogjava.net/orangelizq/archive/2008/09/05/227175.html</link><dc:creator>桔子汁</dc:creator><author>桔子汁</author><pubDate>Fri, 05 Sep 2008 03:46:00 GMT</pubDate><guid>http://www.blogjava.net/orangelizq/archive/2008/09/05/227175.html</guid><wfw:comment>http://www.blogjava.net/orangelizq/comments/227175.html</wfw:comment><comments>http://www.blogjava.net/orangelizq/archive/2008/09/05/227175.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/orangelizq/comments/commentRss/227175.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/orangelizq/services/trackbacks/227175.html</trackback:ping><description><![CDATA[<span class="postbody"><span style="font-size: 15px; line-height: normal"><strong>介绍</strong></span> <br />
<br />
计时器可以提供运行基于时间的工作任务的功能，在计时器的管理下，特定的任务可在某一时间运行一次，也可以按指定的时间间隔反复运行。在众多厂商提供的计时器中应用得比较多的有以下三种： <br />
<br />
● java.util.Timer <br />
Sun JDK 提供的一种轻量级的计时器。 <br />
<br />
● Commonj Timer <br />
IBM 和 BEA 联合制定和推出的一种适用于 J2EE 环境的计时器。<br />
&nbsp;<br />
● WebSphere Application Server Scheduler <br />
IBM WebSphere Application Server 提供的一种功能强大的计时器。 <br />
<br />
<br />
<span style="font-size: 15px; line-height: normal"><strong>java.util.Timer</strong> <br />
</span><br />
java.util.Timer 是 Sun JDK 提供的一种计时器，用于使后台线程按计划执行指定任务，这些任务可以被执行一次，也可以被定期执行。每个 Timer 对象对应一个后台线程，顺序地执行所有计时器任务。如果完成某个计时器任务的时间太长，那么它会&#8220;独占&#8221;计时器的任务执行线程，从而可能延迟后续任务的执行。对 Timer 对象最后的引用完成并且所有未处理的任务都已执行完成后，计时器的任务执行线程会正常终止（并且成为垃圾回收的对象）。</span><br />
<br />
java.util.Timer 简单易用，比较适合提供轻量级的计时器功能。由于其创建的线程会超出容器的管理范围，因此不能应用于管理的环境中。如果用户需要在 J2EE 环境中提供计时器功能，可考虑使用后面即将介绍的 Commonj Timer 或 WebSphere Application Server Scheduler。 <br />
<br />
<br />
<br />
<span style="font-size: 15px; line-height: normal"><strong>Commonj Timer</strong></span> <br />
<br />
Commonj Timer 是 Commonj 规范的一部分，它由 IBM 和 BEA 联合制定和推出，用以更好的响应客户和独立软件商的需求，给开发人员在开发可移植的服务端应用程序时提供一些更加简单和功能更加强大的方法。这个规范主要包括以下几个部分：Service Component Architecture，Service Data Objects，Work Manager and Timer 和 Enterprise Metadata Discovery。其中，Work Manager and Time 为在应用服务器中支持并发任务的执行提供了一些简单 API。这使用户可以方便地在 Servlet 和 EJB 中执行并发的计划任务，从而提高呑吐量，缩短服务端程序的响应时间，很好地解决了在 J2EE 环境中执行用户自定义的多线程并发与计时器服务的问题。 <br />
<br />
Commonj Timer API 包括三个接口：TimerManager, Timer 和 TimerListener。应用程序可以通过 TimerManager 来定期调用 TimerListener。每个 TimerManager 的 shcedule 方法返回一个 Timer 对象。用户可以通过 TimerManager 的 JNDI 名称在管理环境的上下文中查找 TimerManager。 <br />
<br />
<br />
<br />
java.util.Timer、 CommonJ Timer 和 WebSphere Application Server Scheduler 为用户提供了不同级别的、适用与不同范围的计时器，用户可以根据各自的需求使用不同的计时器，表 1 列出了 java.util.Timer、Commonj Timer 和 WebSphere Application Server Scheduler 之间比较结果，用户在使用计时器时可以用来参照比较。 <br />
<br />
表 1. 计时器比较结果 <br />
<strong>java.util.Timer</strong> <br />
来源：Sun <br />
优点：易于使用轻量级 <br />
缺点：创建的线程会超出容器管理范围 <br />
适用范围：非 J2EE 环境 <br />
<strong><br />
Commonj Timer</strong> <br />
来源：BEA and IBM <br />
优点：解决了 java.util.Timer 创建的线程超出容器管理范围的问题；不同于 JMX Timer Service 与 JMX framework 之间的紧耦合，提供了更加友好和独立的 API <br />
缺点：timer 是瞬时的、非事务性的，并且运行于创建它的 JVM 中，不适合于集群环境 <br />
适用范围：J2EE 普通环境 <br />
<br />
<strong>java.util.Timer</strong> <br />
来源：WebSphere Application Server Scheduler <br />
优点：易于管理 具有持久性和事务性 具有灵活的时间定制方式 具有扩展性，适用于集群环境 <br />
缺点：与 WebSphere Application Server 紧耦合 <br />
适用范围：J2EE 普通和集群环境 <br />
<br />
详见：http://www.ibm.com/developerworks/cn/java/j-lo-timer/index.html<br />
</span><!-- Attachments -->
<img src ="http://www.blogjava.net/orangelizq/aggbug/227175.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/orangelizq/" target="_blank">桔子汁</a> 2008-09-05 11:46 <a href="http://www.blogjava.net/orangelizq/archive/2008/09/05/227175.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>[转]java Serialization</title><link>http://www.blogjava.net/orangelizq/archive/2008/09/05/227169.html</link><dc:creator>桔子汁</dc:creator><author>桔子汁</author><pubDate>Fri, 05 Sep 2008 03:26:00 GMT</pubDate><guid>http://www.blogjava.net/orangelizq/archive/2008/09/05/227169.html</guid><wfw:comment>http://www.blogjava.net/orangelizq/comments/227169.html</wfw:comment><comments>http://www.blogjava.net/orangelizq/archive/2008/09/05/227169.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/orangelizq/comments/commentRss/227169.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/orangelizq/services/trackbacks/227169.html</trackback:ping><description><![CDATA[<div id="BlogArticleDetail" style="font-size: 14px"><strong><font size="3">1. 什么是Serialization?</font></strong><br />
<br />
串行化(Serialization)是计算机科学中的一个概念，它是指将对象存储到介质（如文件、内在缓冲区等）中或是以二进制方式通过网络传输。之后可以通过反串行化从这些连续的位数据重新构建一个与原始对象状态相同的对象，因此在特定情况下也可以说是得到一个副本，但并不是所有情况都这样。<br />
<br />
Java有Serialization API为开发者提供了一种标准的机制来串行化类。<br />
<br />
<font color="#c0c0c0"><font color="#000000"><strong><font size="3">2. 为什么要Serilzation?</font></strong></font><br />
<br />
</font>特别地，串行化主要有三种用途：<br />
1）作为一种持久化机制<br />
&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> 如果使用的是FileOutputStream流的方式，则数据将被自动地写入文件中，<br />
2）作为一种复制机制<br />
&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> 如果使用的是<code class="Literal">ByteArrayOutputStream</code>流的方式，数据将写入内存中的字节数组中。该字节数组可以用来创建初始对象的副本，<br />
3）作为一种通信机制<br />
&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> 如果是使用套接字（Socket）流的方式，则数据自动地通过网络连接传输一另一个端点，并由这个端点上的程序来决定做什么。<br />
<br />
<font size="3"><strong>3. Serialization的基本用法</strong></font><strong>：<font size="3">默认机制</font></strong><br />
<br />
将要串行化的类必须实现java.io.Serializable接口，或者是继承实现了该接口的类。然后通过java.io.ObjectOutputStream类来实现持久化，如果用保存到文件上还需要用到java.io.FileOutputStream类。因为ObjectOutputStream被认为是java.io包中的高级类所以可用它来包装低级的类FileOutputStream。在持久化过程中调用的一个方法是ObjectOutputStream对象的writeObject(obj)方法。<br />
<br />
当要从文件中恢复对象时，则是使用java.io.OjbectInputStream与FileInputStream类，调用一方法是ObjectInputStream对象的readObject()方法。<br />
<br />
<strong>示例1：</strong><br />
<font color="#808080"><br />
import java.io.*;<br />
<br />
public class Cat implements Serializable {<br />
&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> private String name;<br />
&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> public Cat () {<br />
&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> this.name = "new cat";<br />
&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> }<br />
<br />
&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> public String getName() {<br />
&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> return this.name;<br />
&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> }<br />
<br />
&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> public void setName(String name) {<br />
&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> this.name = name;<br />
&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> }<br />
}<br />
<br />
<br />
import java.io.*;<br />
<br />
public class CatDemo {<br />
&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> public static void main(String[] args) {<br />
&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> Cat cat = new Cat();<br />
&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> try { //串行化<br />
&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> FileOutputStream fos = new FileOutputStream("catDemo.out");<br />
&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> ObjectOutputStream oos = new ObjectOutputStream(fos);<br />
&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> System.out.println(" 1&gt; " + cat.getName());<br />
&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> cat.setName("My Cat"); &nbsp;<wbr>&nbsp;<wbr><br />
&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> oos.writeObject(cat);<br />
&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> oos.close();&nbsp;<wbr>&nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr><br />
&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> } catch (IOException ex) {<br />
&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> ex.printStackTrace();<br />
&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> }<br />
<br />
&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> try { //反串行化<br />
&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> FileInputStream fis = new FileInputStream("catDemo.out");<br />
&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> ObjectInputStream ois = new ObjectInputStream(fis);<br />
<br />
&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> cat = (Cat) ois.readObject();<br />
&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr><br />
&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> System.out.println(" 2&gt; " + cat.getName());<br />
&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> ois.close();<br />
&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> } catch (IOException ex) {<br />
&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> ex.printStackTrace();<br />
&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> }<code>catch(ClassNotFoundException ex) {&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr><br />
&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> ex.printStackTrace();<br />
&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> }</code><br />
&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> }<br />
}</font><br />
<br />
<br />
<font size="3"><strong>4. Serialization常见问题</strong></font><br />
<br />
正如前面提到的，所有可串行化的类必须直接或是通过继承方式间接地实现java.io.Serializable接口，由于Object类关没有实现这个接口，所以并不是所有类的对象都是可串行化的。像AWT与Swing的GUI组件、字符串、数组等都是可串行化的，而像一些系统级的类（Thread<code>，OutputStream</code> 等）和Socket类是不可串行化的。<br />
<br />
<font color="#0000ff">问题一：如果在一个可串行化的类中Has-As不可串行化的类该怎么处理？</font><br />
在这种情况下在运行时会抛出NotSerializableException<wbr>。<br />
<br />
为了解决类似问题，Java中提供了transient关键字来跳过对不可串行化类的对象的处理。但这依然可能会引起一些问题，在反串行化时，被标识为transient变量不会恢复到其原始状态，而是提供默认值，如示例２中的pig引用将赋值为null，age变量赋值为0;<br />
<br />
<em>附：基本类型和引用类型的默认值<br />
对象引用：null<br />
byte, short, int, long ：0<br />
float, double：0.0<br />
boolean：false<br />
char：'\u0000'(这是Unicode字符集的空格)</em><br />
<br />
<strong>示例2：</strong><br />
<code><font color="#808080">import java.io.*;<br />
public class NewPig2 implements Serializable {<br />
&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> private String newName;<br />
&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> private <font color="#0000ff"><strong>transient</strong></font> Pig pig = new Pig();<br />
&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> private <font color="#0000ff"><strong>transient</strong></font> int age = 2;&nbsp;<wbr>&nbsp;<wbr><br />
&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> public NewPig2() {<br />
&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> newName = "new Pig 2";<br />
&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> //pig = new Pig();<br />
&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> }<br />
&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> public Pig getPig() {<br />
&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> return this.pig;<br />
&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> }<br />
&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> public void setName(String name) {<br />
&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> this.newName = name;<br />
&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> }<br />
&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> public String getName() {<br />
&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> return this.newName;<br />
&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> }<br />
&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> public int getAge() {<br />
&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> return this.age;<br />
&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> }<br />
}<br />
</font></code><code><br />
<font color="#0000ff">问题二：如果父类不可串行化，子类实现了Serializable会怎样？</font><br />
如果有一个Animal类是不可串行化的，而有一个Dog类继承自Animal类并且实现了Serializabl接口，则没有串行化时没有任何问题，但是在反串行化时将会重新调用Animal的构造函数，如示例3所示。<br />
<br />
示例3的运行结果如下：</code><code><br />
1&gt; No Color - new Dog<br />
2&gt; Green - My Dog<br />
4&gt; No Color - My Dog<br />
因为Animal不可串行化，所以必须运行构造函数，但不会在实现Serializable的反串行化类上运行构造函数。<br />
</code><code><br />
示例3：<br />
<font color="#808080"><br />
public class Animal {<br />
&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> private String color;<br />
&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> <font color="#333399">public Animal () {<br />
&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> this.color = "No Color";<br />
&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> }</font><br />
&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> public void setColor(String color) {<br />
&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> this.color = color;<br />
&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> }<br />
&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> public String getColor () {<br />
&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> return this.color;<br />
&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> }<br />
}<br />
<br />
<br />
import java.io.*;<br />
public class Dog extends Animal implements Serializable {<br />
&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> private String name;<br />
&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> public Dog () {<br />
&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> this.name = "new Dog";<br />
&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> }<br />
&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> public String getName() {<br />
&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> return this.name;<br />
&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> }<br />
&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> public void setName(String name) {<br />
&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> this.name = name;<br />
&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> }<br />
}<br />
<br />
import java.io.*;<br />
public class DogTest {<br />
&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> public static void main(String[] args) {<br />
&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> Dog dog = new Dog();&nbsp;<wbr>&nbsp;<wbr><br />
&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> System.out.println(" 1&gt; " + dog.getColor() + " - " + dog.getName());<br />
&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> dog.setColor("Green");<br />
&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> dog.setName("My Dog");<br />
&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> System.out.println(" 2&gt; " + dog.getColor() + " - " + dog.getName());<br />
&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> try {//串行化<br />
&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> FileOutputStream fos = new FileOutputStream("myDog.out");<br />
&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> ObjectOutputStream oos = new ObjectOutputStream(fos);<br />
&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> oos.writeObject(dog);<br />
&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> oos.close();<br />
&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> } catch (Exception ex) {<br />
&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> ex.printStackTrace();<br />
&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> }<br />
&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> try {//反串行化<br />
&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> FileInputStream fis = new FileInputStream("myDog.out");<br />
&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> ObjectInputStream ois = new ObjectInputStream(fis);<br />
&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> dog = (Dog) ois.readObject(); &nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr><br />
&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> System.out.println(" 4&gt; " + dog.getColor() + " - " + dog.getName());<br />
&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> ois.close();<br />
&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> } catch (Exception ex) {<br />
&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> ex.printStackTrace();<br />
&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> }<br />
&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> }<br />
}<br />
<br />
</font><strong><font size="3">5.</font></strong></code><strong>自定义默认方法：使用writeOject和readObject<br />
</strong><font size="2"><br />
假如在一个可以串行化的类中有一个不可串行化的对象，但又想保存该对象的状态信息该如何是好？在这样情况下可以在这个可串行化的类中实现writeObject()和readObject()。<br />
<br />
<strong>示例4:</strong><br />
<font color="#808080">import java.io.*;<br />
import java.util.Scanner;<br />
<br />
public class NewPig3 implements Serializable {<br />
&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> private String newName;<br />
&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> private transient Pig pig = new Pig();<br />
&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> private transient int age = 2;<br />
&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr><br />
&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> public NewPig3() {<br />
&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> newName = "new Pig 3";<br />
&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> //pig = new Pig();<br />
&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> }<br />
<br />
&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> <font color="#0000ff"><strong>private void writeObject(ObjectOutputStream oos) throws IOException {</strong></font><br />
&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> oos.defaultWriteObject();<br />
&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> oos.writeChars(pig.getName());<br />
&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> //oos.writeInt(this.age);<br />
&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> }<br />
<br />
&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> <font color="#0000ff"><strong>private void readObject(ObjectInputStream ois) throws IOException, ClassNotFoundException {</strong></font><br />
&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> ois.defaultReadObject();<br />
&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> pig = new Pig();<br />
&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> Scanner in = new Scanner(ois);<br />
&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> if (in.hasNextLine()) {&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr><br />
&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> pig.setName(in.nextLine());<br />
&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> }<br />
&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> }<br />
<br />
&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> public Pig getPig() {<br />
&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> return this.pig;<br />
&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> }<br />
<br />
&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> public void setName(String name) {<br />
&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> this.newName = name;<br />
&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> }<br />
<br />
&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> public String getName() {<br />
&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> return this.newName;<br />
&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> }<br />
<br />
&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> public int getAge() {<br />
&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> return this.age;<br />
&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> }<br />
}<br />
</font></font></div>
<img src ="http://www.blogjava.net/orangelizq/aggbug/227169.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/orangelizq/" target="_blank">桔子汁</a> 2008-09-05 11:26 <a href="http://www.blogjava.net/orangelizq/archive/2008/09/05/227169.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>[转]java中byte转换int时为何与0xff进行与运算</title><link>http://www.blogjava.net/orangelizq/archive/2008/07/20/216228.html</link><dc:creator>桔子汁</dc:creator><author>桔子汁</author><pubDate>Sun, 20 Jul 2008 12:36:00 GMT</pubDate><guid>http://www.blogjava.net/orangelizq/archive/2008/07/20/216228.html</guid><wfw:comment>http://www.blogjava.net/orangelizq/comments/216228.html</wfw:comment><comments>http://www.blogjava.net/orangelizq/archive/2008/07/20/216228.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/orangelizq/comments/commentRss/216228.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/orangelizq/services/trackbacks/216228.html</trackback:ping><description><![CDATA[在剖析该问题前请看如下代码<br />
public static String bytes2HexString(byte[] b) {<br />
&nbsp;&nbsp;String ret = "";<br />
&nbsp;&nbsp;for (int i = 0; i &lt; b.length; i++) {<br />
&nbsp; &nbsp;String hex = Integer.toHexString(b[ i ] &amp; 0xFF);<br />
&nbsp; &nbsp;if (hex.length() == 1) {<br />
&nbsp; &nbsp; hex = '0' + hex;<br />
&nbsp; &nbsp;}<br />
&nbsp; &nbsp;ret += hex.toUpperCase();<br />
&nbsp;&nbsp;}<br />
&nbsp;&nbsp;return ret;<br />
}<br />
上面是将byte[]转化十六进制的字符串,注意这里b[ i ] &amp; 0xFF将一个byte和 0xFF进行了与运算,然后使用Integer.toHexString取得了十六进制字符串,可以看出<br />
b[ i ] &amp; 0xFF运算后得出的仍然是个int,那么为何要和 0xFF进行与运算呢?直接 Integer.toHexString(b[ i ]);,将byte强转为int不行吗?答案是不行的.<br />
<br />
其原因在于:<br />
1.byte的大小为8bits而int的大小为32bits<br />
2.java的二进制采用的是补码形式<br />
<br />
在这里先温习下计算机基础理论<br />
<br />
byte是一个字节保存的，有8个位，即8个0、1。<br />
8位的第一个位是符号位， <br />
也就是说0000 0001代表的是数字1 <br />
1000 0000代表的就是-1 <br />
所以正数最大位0111 1111，也就是数字127 <br />
负数最大为1111 1111，也就是数字-128<br />
<br />
上面说的是二进制原码，但是在java中采用的是补码的形式，下面介绍下什么是补码<br />
<br />
1、反码：<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;一个数如果是正，则它的反码与原码相同；<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;一个数如果是负，则符号位为1，其余各位是对原码取反；<br />
<br />
2、补码：利用溢出，我们可以将减法变成加法<br />
&nbsp; &nbsp;&nbsp; &nbsp; 对于十进制数，从9得到5可用减法：<br />
&nbsp; &nbsp;&nbsp; &nbsp; 9－4＝5&nbsp; &nbsp; 因为4+6＝10，我们可以将6作为4的补数<br />
&nbsp; &nbsp;&nbsp; &nbsp; 改写为加法：<br />
&nbsp; &nbsp;&nbsp; &nbsp; 9+6＝15（去掉高位1，也就是减10）得到5.<br />
<br />
&nbsp; &nbsp;&nbsp; &nbsp; 对于十六进制数，从c到5可用减法：<br />
&nbsp; &nbsp;&nbsp; &nbsp; c－7＝5&nbsp; &nbsp; 因为7+9＝16 将9作为7的补数<br />
&nbsp; &nbsp;&nbsp; &nbsp; 改写为加法：<br />
&nbsp; &nbsp;&nbsp; &nbsp; c+9＝15（去掉高位1，也就是减16）得到5.<br />
<br />
&nbsp; &nbsp; 在计算机中，如果我们用1个字节表示一个数，一个字节有8位，超过8位就进1，在内存中情况为（100000000），进位1被丢弃。<br />
<br />
&nbsp; &nbsp; ⑴一个数为正，则它的原码、反码、补码相同<br />
&nbsp; &nbsp; ⑵一个数为负，刚符号位为1，其余各位是对原码取反，然后整个数加1<br />
&nbsp; &nbsp; <br />
- 1的原码为&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; 10000001<br />
- 1的反码为&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; 11111110<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;+ 1<br />
- 1的补码为&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; 11111111<br />
<br />
0的原码为&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;00000000<br />
0的反码为&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;11111111（正零和负零的反码相同）<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;+1<br />
0的补码为&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;100000000（舍掉打头的1，正零和负零的补码相同）<br />
<br />
Integer.toHexString的参数是int，如果不进行&amp;0xff，那么当一个byte会转换成int时，由于int是32位，而byte只有8位这时会进行补位，<br />
例如补码11111111的十进制数为-1转换为int时变为11111111111111111111111111111111好多1啊，呵呵！即0xffffffff但是这个数是不对的，这种补位就会造成误差。<br />
和0xff相与后，高24比特就会被清0了，结果就对了。<br />
<br />
----<br />
Java中的一个byte，其范围是-128~127的，而Integer.toHexString的参数本来是int，如果不进行&amp;0xff，那么当一个byte会转换成int时，对于负数，会做位扩展，举例来说，一个byte的-1（即0xff），会被转换成int的-1（即0xffffffff），那么转化出的结果就不是我们想要的了。<br />
<br />
而0xff默认是整形，所以，一个byte跟0xff相与会先将那个byte转化成整形运算，这样，结果中的高的24个比特就总会被清0，于是结果总是我们想要的。<br />
<br />
<img src ="http://www.blogjava.net/orangelizq/aggbug/216228.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/orangelizq/" target="_blank">桔子汁</a> 2008-07-20 20:36 <a href="http://www.blogjava.net/orangelizq/archive/2008/07/20/216228.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>