<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
  <channel>
    <title>但行好事 莫问前程</title>
    <description>The best way to predict the future is to invent it.</description>
    <link>http://simohayha.javaeye.com</link>
    <language>UTF-8</language>
    <copyright>Copyright 2003-2008, JavaEye.com</copyright>
    <docs>http://blogs.law.harvard.edu/tech/rss</docs>
    <generator>JavaEye - 做最棒的软件开发交流社区</generator>
      <item>
        <title>wild world--skins的片尾曲</title>
        <author>simohayha</author>
        <description>
          <![CDATA[
          <br/>
          作者: <a href="http://simohayha.javaeye.com">simohayha</a>&nbsp;
          链接：<a href="http://simohayha.javaeye.com/blog/200894" style="color:red;">http://simohayha.javaeye.com/blog/200894</a>&nbsp;
          发表时间: 2008年06月06日
          <br/><br/>
          声明：本文系JavaEye网站发布的原创博客文章，未经作者书面许可，严禁任何网站转载本文，否则必将追究法律责任！
          <br/><br/>
          <p>
<object height="300" classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0" width="400">
<param name="src" value="http://www.tudou.com/v/ryh_ILVfiGQ" /><embed src="http://www.tudou.com/v/ryh_ILVfiGQ" type="application/x-shockwave-flash" height="300" width="400"></embed>
</object>
</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>英剧skins的第一季的最后一集的片尾曲，非常棒地说。</p>
<p>&nbsp;</p>
<p>这部片也非常好看的说，强烈推荐。</p>
<p>&nbsp;</p>
<p><img src="http://otho.douban.com/lpic/s2764331.jpg" height="435" alt="" width="306" /></p>
          <br/>
          <span style="color:red;">
            <a href="http://simohayha.javaeye.com/blog/200894#comments" style="color:red;">本文的讨论也很精彩，浏览讨论>></a>
          </span>
          <br/><br/><br/>
          <span style="color:#E28822;">JavaEye推荐</span>
          <br/>
          <ul class='adverts'><li><a href='/adverts/41' target='_blank'><span style="color:red;font-weight:bold;">北京: 千橡集团暨校内网诚聘软件研发工程师</span></a></li><li><a href='/adverts/92' target='_blank'><span style="color:red;font-weight:bold;">快来参加7月17日在成都举行的SOA中国技术论坛</span></a></li><li><a href='/adverts/97' target='_blank'><span style="color:blue;font-weight:bold;">Oracle专区上线，有Oracle最新文章，重要下载及知识库等精彩内容，欢迎访问。</span></a></li><li><a href='/adverts/106' target='_blank'><span style="color:blue;font-weight:bold;">JavaEye问答大赛开始了！ 从6月23日 至 7月6日，奖品丰厚 ！</span></a></li><li><a href='/adverts/42' target='_blank'><span style="color:red;font-weight:bold;">搜狐网站诚聘Java、PHP和C++工程师</span></a></li></ul>
          <br/><br/><br/>
          ]]>
        </description>
        <pubDate>Fri, 06 Jun 2008 13:25:12 +0800</pubDate>
        <link>http://simohayha.javaeye.com/blog/200894</link>
        <guid>http://simohayha.javaeye.com/blog/200894</guid>
      </item>
      <item>
        <title>一个创建闭包的小技巧</title>
        <author>simohayha</author>
        <description>
          <![CDATA[
          <br/>
          作者: <a href="http://simohayha.javaeye.com">simohayha</a>&nbsp;
          链接：<a href="http://simohayha.javaeye.com/blog/200304" style="color:red;">http://simohayha.javaeye.com/blog/200304</a>&nbsp;
          发表时间: 2008年06月05日
          <br/><br/>
          声明：本文系JavaEye网站发布的原创博客文章，未经作者书面许可，严禁任何网站转载本文，否则必将追究法律责任！
          <br/><br/>
          一个小技巧，在Ola Bini 的blog上看到的。<br /><br />假设你想要这样一个类：<br />创建这个类的时候send一个block给它，然后在后面可以通过某个方法名来调用这个block。<br /><pre name="code" class="ruby">class DoSomething 
  def initialize   
    (class &lt;&lt; self; self; end).send :define_method, :call do     
      yield   
    end 
  end
end
d = DoSomething.new do puts "hello world"
  end
d.call
d.call</pre>
          <br/>
          <span style="color:red;">
            <a href="http://simohayha.javaeye.com/blog/200304#comments" style="color:red;">本文的讨论也很精彩，浏览讨论>></a>
          </span>
          <br/><br/><br/>
          <span style="color:#E28822;">JavaEye推荐</span>
          <br/>
          <ul class='adverts'><li><a href='/adverts/97' target='_blank'><span style="color:blue;font-weight:bold;">Oracle专区上线，有Oracle最新文章，重要下载及知识库等精彩内容，欢迎访问。</span></a></li><li><a href='/adverts/106' target='_blank'><span style="color:blue;font-weight:bold;">JavaEye问答大赛开始了！ 从6月23日 至 7月6日，奖品丰厚 ！</span></a></li><li><a href='/adverts/92' target='_blank'><span style="color:red;font-weight:bold;">快来参加7月17日在成都举行的SOA中国技术论坛</span></a></li><li><a href='/adverts/42' target='_blank'><span style="color:red;font-weight:bold;">搜狐网站诚聘Java、PHP和C++工程师</span></a></li><li><a href='/adverts/41' target='_blank'><span style="color:red;font-weight:bold;">北京: 千橡集团暨校内网诚聘软件研发工程师</span></a></li></ul>
          <br/><br/><br/>
          ]]>
        </description>
        <pubDate>Thu, 05 Jun 2008 00:12:10 +0800</pubDate>
        <link>http://simohayha.javaeye.com/blog/200304</link>
        <guid>http://simohayha.javaeye.com/blog/200304</guid>
      </item>
      <item>
        <title>解决Colored Cubes问题</title>
        <author>simohayha</author>
        <description>
          <![CDATA[
          <br/>
          作者: <a href="http://simohayha.javaeye.com">simohayha</a>&nbsp;
          链接：<a href="http://simohayha.javaeye.com/blog/199324" style="color:red;">http://simohayha.javaeye.com/blog/199324</a>&nbsp;
          发表时间: 2008年06月02日
          <br/><br/>
          声明：本文系JavaEye网站发布的原创博客文章，未经作者书面许可，严禁任何网站转载本文，否则必将追究法律责任！
          <br/><br/>
          Engineering Puzzle<br /><br />You have four colored cubes. Each side of each cube is a single color,<br />and there are four colors: blue (B), red (R), green (G) and yellow (Y)<br />Describing the six faces as front, back, left, right, top, bottom, the<br />cube colors are:<br /><br />Front  Back Left Right Top Bottom<br />1    R     B    G    Y     B    Y<br />2    R     G    G    Y     B    B<br />3    Y     B    R    G     Y    R<br />4    Y     G    B    R     R    R<br /><br /><br />The objective is to find ways to stack the four cubes as a vertical<br />column so that each side of the column is showing all four colors.<br /><br /><br /><pre name="code" class="ruby">#cube对象，其中包含一个Color属性，此属性是一个Struct对象，依次为所有边的颜色
class Cube
  attr_accessor :color

  def initialize(color)
    @color=color
    @rotation      = 0
    @times_rotated = 0
  end

#这里我们可以设置一个三维坐标，每个盒子的24中摆放方式就是通过这个3为坐标的不同轴的旋转得到。
  def rotate
    @times_rotated = @times_rotated + 1
    
    
    #y轴的旋转
    tmp_top = @color.Top
    @color.Top=@color.Left
    @color.Left=@color.Bottom
    @color.Bottom=@color.Right
    @color.Right=tmp_top
#当为4的倍数时意味着已经回到开始的位置，因此需要变换坐标轴
    if @times_rotated % 4 == 0
      tmp_front = @color.Front
#当为2的倍数时意味着又一次要回到刚才已经变换过得位置，因此未免重复，需要再次变换坐标轴
      if @rotation % 2 == 0
        #x轴的旋转
        @color.Front=@color.Bottom
        @color.Bottom=@color.Back
        @color.Back=@color.Top
        @color.Top=tmp_front
      else
        #z轴的旋转
        @color.Front=@color.Right
        @color.Right=@color.Back
        @color.Back=@color.Left
        @color.Left=tmp_front
      end

      @rotation = @rotation + 1
    end
  end
  

#打印出cube
  def show(name = "")
    puts name
    
    puts  &lt;&lt;-EOF 
    Front :   #{@color[:Front]}  Back  :   #{@color[:Back]}  Left   :   #{@color[:Left]}  Right :   #{@color[:Right]} Top   :   #{@color[:Top]} Bottom:  #{@color[:Bottom]}
    EOF
    
  end
  
end

#将4个cube组合为一个cube_box对象
class Cube_Box
  def initialize(cube_a, cube_b, cube_c, cube_d)
    @cube_a = cube_a
    @cube_b = cube_b
    @cube_c = cube_c
    @cube_d = cube_d
  end
  
  
  #判断是否符合条件
  def valid?
    side_valid?(:Front)&&side_valid?(:Back)&&side_valid?(:Left)&&side_valid?(:Right)
  end
  
  def side_valid? side
    (@cube_a.color[side] != @cube_b.color[side]) && (@cube_a.color[side] != @cube_c.color[side]) &&(@cube_a.color[side] != @cube_d.color[side])&& (@cube_b.color[side] != @cube_c.color[side])&& (@cube_b.color[side]!= @cube_d.color[side])&& (@cube_c.color[side] != @cube_d.color[side])
  end
  
  #打印出结果
  def show_box
    p "********************************************************************"
    @cube_a.show("cube_a")
    @cube_b.show("cube_b")
    @cube_c.show("cube_c")
    @cube_d.show("cube_d")
    p "********************************************************************"
  end
end

#构造每个cube的color对象
Color=Struct.new("Color",:Front,:Back,:Left,:Right,:Top,:Bottom)
color_a=Color.new("r","b","g","y","b","y")
color_b=Color.new("r","g","g","y","b","b")
color_c=Color.new("y","b","r","g","y","r")
color_d=Color.new("y","g","b","r","r","r")

#通过color对象构造cube对象
cube_a=Cube.new(color_a)
cube_b=Cube.new(color_b)
cube_c=Cube.new(color_c)
cube_d=Cube.new(color_d)

#符合结果的组合的数目
result_numbers=0

#由于每个cube有p3,3 * 4=24种因此这边要进行24^4次循环，找到合适的后调用show_box打印出来。
24.times do
  24.times do
    24.times do
      24.times do
        cube_box_temp=Cube_Box.new(cube_a,cube_b,cube_c,cube_d)
        if cube_box_temp.valid?
          cube_box_temp.show_box
          result_numbers = result_numbers + 1
        end
        cube_d.rotate
      end
      cube_c.rotate
     end
    cube_b.rotate
  end
  cube_a.rotate
end

puts "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
puts "Number of found results: " + result_numbers.to_s</pre>
          <br/>
          <span style="color:red;">
            <a href="http://simohayha.javaeye.com/blog/199324#comments" style="color:red;">本文的讨论也很精彩，浏览讨论>></a>
          </span>
          <br/><br/><br/>
          <span style="color:#E28822;">JavaEye推荐</span>
          <br/>
          <ul class='adverts'><li><a href='/adverts/42' target='_blank'><span style="color:red;font-weight:bold;">搜狐网站诚聘Java、PHP和C++工程师</span></a></li><li><a href='/adverts/97' target='_blank'><span style="color:blue;font-weight:bold;">Oracle专区上线，有Oracle最新文章，重要下载及知识库等精彩内容，欢迎访问。</span></a></li><li><a href='/adverts/92' target='_blank'><span style="color:red;font-weight:bold;">快来参加7月17日在成都举行的SOA中国技术论坛</span></a></li><li><a href='/adverts/106' target='_blank'><span style="color:blue;font-weight:bold;">JavaEye问答大赛开始了！ 从6月23日 至 7月6日，奖品丰厚 ！</span></a></li><li><a href='/adverts/41' target='_blank'><span style="color:red;font-weight:bold;">北京: 千橡集团暨校内网诚聘软件研发工程师</span></a></li></ul>
          <br/><br/><br/>
          ]]>
        </description>
        <pubDate>Mon, 02 Jun 2008 10:43:11 +0800</pubDate>
        <link>http://simohayha.javaeye.com/blog/199324</link>
        <guid>http://simohayha.javaeye.com/blog/199324</guid>
      </item>
      <item>
        <title>代码大全读书笔记1</title>
        <author>simohayha</author>
        <description>
          <![CDATA[
          <br/>
          作者: <a href="http://simohayha.javaeye.com">simohayha</a>&nbsp;
          链接：<a href="http://simohayha.javaeye.com/blog/187129" style="color:red;">http://simohayha.javaeye.com/blog/187129</a>&nbsp;
          发表时间: 2008年04月26日
          <br/><br/>
          声明：本文系JavaEye网站发布的原创博客文章，未经作者书面许可，严禁任何网站转载本文，否则必将追究法律责任！
          <br/><br/>
          这么好的书，觉得写点东西，记录一下比较好。<br /><br />4.1选择编程语言。<br /><br />程序员使用熟悉的编程语言生产率高于使用不熟悉的编程语言，并且高级语言比低级语言的效率要高得多，作者列出了几种高级语言的每一行代码与等效的c语言代码行数之比。比如java是2.5，而perl和python就是6.而且编程语言影响程序员的思维随处可见，作者举了个他遇到的项目，那就是一群fortran程序员去写c++，最后写出来的代码就是伪装成c++的fortran代码。<br /><br /><span style="color: red">这个很有感触，自己第一个项目当时有用到c，而当时项目组里面只有一个人对c比较熟悉，可是没办法，人不够，只好上我们几个做java的，最后开发效率非常低下，而且代码写的及其丑陋，问题一大堆，就一个c里面的字符串，把我们几个java程序员就给郁闷了好久。<br /><br />说道表达能力，象python，perl，ruby之类的脚本语言是c或者java之类的不能比的，就像上面作者给出的统计，一行脚本语言比一行c或者java代码要完成的工作多得多，所以很多游戏都会采用脚本作为一个辅助。<br /><br />上次看到一篇文章就是讲java的标准库里面的一段c风格的java代码：</span><br /><br /><div class="quote_title">引用</div><div class="quote_div">Occasionally in Java, you come across an API that makes you sit up and go “What were they thinking?” Take, for example, the code to list all the threads in the current ThreadGroup. Rather than having the obvious method: i.e. one that returns a list (or array) of threads, the signature looks like this: <br /><br />int enumerate(Thread[] list) <br /><br />You pass an empty array to the method, which will be filled with Thread objects. The method then returns the number of threads it placed in the array. If the array is not long enough to accept all the threads, the overflow will be silently discarded. <br /><br />To initialise the array, you must rely on ThreadGroup#activeCount, which only returns an approximation of the number of threads that enumerate might return. <br /><br />If you’re looking to avoid memory leaks in a non-garbage-collected environment, then it makes perfect sense for an API to push responsibility for memory management back up its caller, and to gracefully handle whichever buffer-size it’s given to fill. When you’ve got pervasive GC, it just looks (and is) clumsy. <br /><br />So the obvious answer to “What were they thinking?”, of course, is “They were thinking like C programmers”. <br /><br />Which in turn leads one to suspect that this particular API has been around since before Java was called Java.</div><br /><br /><span style="color: red">我觉得特别是这种有gc的语言的程序员和无gc的语言的程序员，如果让他们互换的话，写出来的程序是最会不伦不类的。</span><br /><br />4.2 编程约定<br /><br />其实很简单，那就是变量名称，类的名称，方法名称，代码的格式，注释的规定等等等等，这些很细节的东西。程序因此也需要底层的完整性。成功编程的一个关键就是避免随意的变化。<br /><br /><span style="color: red">这些我觉得在项目中很重要，比如方法的名称，在我的一个项目中，那些c代码中的名称，有的是c的命名规则，有的是java的命名规则，所以这些代码给人感觉极度混乱和邋遢。</span><br /><br />4.3深入一种语言去编程。<br /><br />作者给出了建议，那就是如果你使用的语言缺乏你所希望用的构件，或者倾向于出现其他种类的问题，那就应该去试着弥补他，发明你自己的编码约定，标准，类库以及其他的改进措施。<br /><br /><span style="color: red">作者这里给出了一个vb的例子，其实像apache 的commons那几个包，或者说像spring，hibernate这些框架也都是深入java去编程的很好的例子。</span>
          <br/>
          <span style="color:red;">
            <a href="http://simohayha.javaeye.com/blog/187129#comments" style="color:red;">本文的讨论也很精彩，浏览讨论>></a>
          </span>
          <br/><br/><br/>
          <span style="color:#E28822;">JavaEye推荐</span>
          <br/>
          <ul class='adverts'><li><a href='/adverts/41' target='_blank'><span style="color:red;font-weight:bold;">北京: 千橡集团暨校内网诚聘软件研发工程师</span></a></li><li><a href='/adverts/92' target='_blank'><span style="color:red;font-weight:bold;">快来参加7月17日在成都举行的SOA中国技术论坛</span></a></li><li><a href='/adverts/97' target='_blank'><span style="color:blue;font-weight:bold;">Oracle专区上线，有Oracle最新文章，重要下载及知识库等精彩内容，欢迎访问。</span></a></li><li><a href='/adverts/42' target='_blank'><span style="color:red;font-weight:bold;">搜狐网站诚聘Java、PHP和C++工程师</span></a></li><li><a href='/adverts/106' target='_blank'><span style="color:blue;font-weight:bold;">JavaEye问答大赛开始了！ 从6月23日 至 7月6日，奖品丰厚 ！</span></a></li></ul>
          <br/><br/><br/>
          ]]>
        </description>
        <pubDate>Sat, 26 Apr 2008 19:16:41 +0800</pubDate>
        <link>http://simohayha.javaeye.com/blog/187129</link>
        <guid>http://simohayha.javaeye.com/blog/187129</guid>
      </item>
      <item>
        <title>Nonblocking I/O</title>
        <author>simohayha</author>
        <description>
          <![CDATA[
          <br/>
          作者: <a href="http://simohayha.javaeye.com">simohayha</a>&nbsp;
          链接：<a href="http://simohayha.javaeye.com/blog/177884" style="color:red;">http://simohayha.javaeye.com/blog/177884</a>&nbsp;
          发表时间: 2008年03月31日
          <br/><br/>
          声明：本文系JavaEye网站发布的原创博客文章，未经作者书面许可，严禁任何网站转载本文，否则必将追究法律责任！
          <br/><br/>
          在Nonblocking I/O中最重要的也就是三个类<br /><br /><div class="quote_title">引用</div><div class="quote_div">java.nio.channels.SelectableChannel<br />java.nio.channels.Selector<br />java.nio.channels.SelectionKey</div><br /><br />1 SelectableChannel<br /><br />默认情况下channels是阻塞的.我们可以设置一个channel为nonblock的,可是并不是所有的channel都可以设置为nonblock的,比如file channels就不能设置为nonblock.所有提供nonblocking I/O的类都是SelectableChannel的子类.<br /><br />我们能够通过configureBlocking方法来设置channel为nonblocking mode.<br /><br /><pre name="code" class="java">public abstract SelectableChannel configureBlocking(boolean block)
 throws IOException</pre><br /><br /><br />当你将一个channel置于nonblocking mode时,你就不能立即读或者写它,你需要将它注册到一个Selector对象.也就是说此时这个channel什么时候读和写,已经完全交给这个selector来控制了.<br /><br />我们使用register方法来注册一个channel到一个Selector对象.<br /><br /><pre name="code" class="java">public abstract SelectionKey register(Selector selector, int operations)
 throws ClosedChannelException</pre><br /><br />这里要注意的是一个channel对象只能被注册到一个Selector对象一次.<br /><br />register方法的第二个参数指的是这个Selector如何处理这个channel,这里有4中操作方式:<br />reading, writing, accepting和connecting.其中我们可以用| 来加入多个操作.Server socket channels是accepting.<br /><br /><pre name="code" class="java">channel.register(selector, SelectionKey.OP_READ | SelectionKey.OP_WRITE);</pre><br /><br />register还有一个三个参数的方法<br /><pre name="code" class="java">
public abstract SelectionKey register(
 Selector selector, int operations, Object attachment)
 throws ClosedChannelException</pre><br /><br />在这里第三个参数可以为空,如果为空也就相当于2个参数的方法了.这个参数也就是存储一个channel读或者写的数据.一般都使用一个ByteBuffer对象来存储这个.<br /><br />register返回一个SelectionKey对象,来表示这个channel与Selector的对象之间的独一无二的关系.<br /><br />register和configureBlocking都是线程安全的.<br /><br />其他的方法,去看jdk文档就行了.<br /><br />2 Selector<br /><br />Selector类可以说是nonblocking I/O中最重要的一个类.因为他能够控制你的程序中的channel什么时候准备好被存取.我们能够使用它的静态方法open来创建一个 Selector对象:<br /><br /><pre name="code" class="java">public static Selector open( ) throws IOException</pre><br /><br />当你想要得到Selector那些channel准备好了的时候,也就是说那些channel能够非阻塞的读或者写.你可以调用下面的三个方法中的一个:<br /><br /><pre name="code" class="java">public abstract int selectNow( ) throws IOException
public abstract int select( ) throws IOException
public abstract int select(long timeout) throws IOException</pre><br /><br />selectNow方法是非阻塞的,而另外两个方法是阻塞的.selectNow方法将会马上返回,哪怕它并没有select到任何东西.而另外两个方法则是select到了channel才会返回,或者线程中断,或者超时也会返回.<br /><br />其实这3个方法说白了,也就是选择出一个准备好io操作的channel的集合.返回值就是准备好的channel的个数.<br /><br />还有一个方法selectedKeys,返回一个java.util.Set ,这个set里面包含所有注册在这个Selector上的准备好io操作的channel的key.<br /><br /><pre name="code" class="java"> public abstract Set&lt;SelectionKey> selectedKeys();</pre><br /><br /><br />这里要注意,一个Selector,当你使用完他时应当调用他的close方法来关闭它:<br /><pre name="code" class="java">public abstract void close( ) throws IOException</pre><br /><br />在一些系统里,一个select()可能会阻塞一个线程一段时间,此时另外的线程能够调用Selector's wakeup方法来使select方法立即返回.jdk文档里是这样写的:<br /><br /><div class="quote_title">引用</div><div class="quote_div">If another thread is currently blocked in an invocation of the select() or select(long) methods then that invocation will return immediately. If no selection operation is currently in progress then the next invocation of one of these methods will return immediately unless the selectNow() method is invoked in the meantime. In any case the value returned by that invocation may be non-zero. Subsequent invocations of the select() or select(long) methods will block as usual unless this method is invoked again in the meantime. </div><br /><br />3 SelectionKey<br /><br />java.nio.channels.SelectionKey 封装了一个channel注册到一个Selector上的所有信息 每一个SelectionKey封装了下列的信息:<br /><br /><div class="quote_title">引用</div><div class="quote_div">The channel<br /><br />The Selector<br /><br />An arbitrary object attachment, normally used to point to the data the channel is reading or writing<br /><br />The operations the channels is interested in performing<br /><br />The operations the channel is currently ready to perform without blocking (or, more accurately, was ready to perform when select( ) was last called)</div><br /><br />这些信息在SelectionKey中大部分都是通过set或者get方法来进行设置和得到的.<br /><br />readyOps方法可以得到这个SelectionKey的mode,也就是说,得到它是read, write, connect, 还是accept.<br /><br /><pre name="code" class="java">public abstract int readyOps( )</pre><br /><br />这里还有一套更方便的api来判断这个:<br /><br /><pre name="code" class="java">public final boolean isReadable( )
public final boolean isWritable( )
public final boolean isConnectable( )
public final boolean isAcceptable( )</pre><br /><br />如果你想读或者写channel,你必须得通过channel方法来得到这个channel然后才能进行操作.<br /><br /><pre name="code" class="java">public abstract SelectableChannel channel( )</pre><br /><br /><br />这里要注意一个channel能有多个key,但是每一个key只能有一个channel.<br /><br />这里还有两个重要的方法:<br /><br /><pre name="code" class="java">public final Object attach(Object ob)
public final Object attachment( )</pre><br /><br />第一个方法attach联系这个对象到这个key(或者用三个参数的register方法也是一样的),第二个方法attachment是从key中得到这个对象.<br /><br />你还能通过cancel方法来撤销一个注册:<br /><br /><pre name="code" class="java">public abstract void cancel( )</pre><br /><br />这边有一个简单的例子:<br /><br /><br /><pre name="code" class="java">import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.util.Iterator;
import java.util.Set;


public class NonBlockDataStuffer {
	private static byte[] data = new byte[256];

	public static void main(String[] args) throws IOException {
		for (int i = 0; i &lt; data.length; i++)
			data[i] = (byte) i;
		ServerSocketChannel server = ServerSocketChannel.open();
		server.configureBlocking(false);
	    server.socket().bind(new InetSocketAddress(9000));
	    Selector selector = Selector.open( );
	    ByteBuffer buffer=ByteBuffer.allocate(256);
	    server.register(selector, SelectionKey.OP_ACCEPT,buffer);
		while (true) {
			selector.select();
			Set&lt;SelectionKey> readyKeys = selector.selectedKeys( );
		    Iterator&lt;SelectionKey> iterator = readyKeys.iterator( );
			while (iterator.hasNext( )) {
		        SelectionKey key = iterator.next( );
		        iterator.remove();
		        try {
		          if (key.isAcceptable()) {
		            SocketChannel client = server.accept();
		            System.out.println("Accepted connection from " + client);
		            client.configureBlocking(false);
		            ByteBuffer source = ByteBuffer.wrap(data);
		            SelectionKey key2 = client.register(selector, SelectionKey.OP_WRITE);
		            key2.attach(source);
		          }
		          else if (key.isWritable( )) {
		            SocketChannel client = (SocketChannel) key.channel( );
		            ByteBuffer output = (ByteBuffer) key.attachment();
		            if (!output.hasRemaining( )) {
		              output.rewind( );
		            }
		            client.write(output);
		          }
		        }
		        catch (IOException ex) {
		          key.cancel( );
		          selector.close();
		          try {
		            key.channel().close();
		          }
		          catch (IOException cex) {}
		        }
		      }

		}
	}
}
</pre><br /><br />其实nio还有一个很重要的类那就是pipe,那个下次再说了...
          <br/>
          <span style="color:red;">
            <a href="http://simohayha.javaeye.com/blog/177884#comments" style="color:red;">本文的讨论也很精彩，浏览讨论>></a>
          </span>
          <br/><br/><br/>
          <span style="color:#E28822;">JavaEye推荐</span>
          <br/>
          <ul class='adverts'><li><a href='/adverts/41' target='_blank'><span style="color:red;font-weight:bold;">北京: 千橡集团暨校内网诚聘软件研发工程师</span></a></li><li><a href='/adverts/106' target='_blank'><span style="color:blue;font-weight:bold;">JavaEye问答大赛开始了！ 从6月23日 至 7月6日，奖品丰厚 ！</span></a></li><li><a href='/adverts/42' target='_blank'><span style="color:red;font-weight:bold;">搜狐网站诚聘Java、PHP和C++工程师</span></a></li><li><a href='/adverts/92' target='_blank'><span style="color:red;font-weight:bold;">快来参加7月17日在成都举行的SOA中国技术论坛</span></a></li><li><a href='/adverts/97' target='_blank'><span style="color:blue;font-weight:bold;">Oracle专区上线，有Oracle最新文章，重要下载及知识库等精彩内容，欢迎访问。</span></a></li></ul>
          <br/><br/><br/>
          ]]>
        </description>
        <pubDate>Mon, 31 Mar 2008 00:10:51 +0800</pubDate>
        <link>http://simohayha.javaeye.com/blog/177884</link>
        <guid>http://simohayha.javaeye.com/blog/177884</guid>
      </item>
      <item>
        <title>一个介绍Grizzly的ppt</title>
        <author>simohayha</author>
        <description>
          <![CDATA[
          <br/>
          作者: <a href="http://simohayha.javaeye.com">simohayha</a>&nbsp;
          链接：<a href="http://simohayha.javaeye.com/blog/176578" style="color:red;">http://simohayha.javaeye.com/blog/176578</a>&nbsp;
          发表时间: 2008年03月27日
          <br/><br/>
          声明：本文系JavaEye网站发布的原创博客文章，未经作者书面许可，严禁任何网站转载本文，否则必将追究法律责任！
          <br/><br/>
          Grizzly看起来很诱人的说..<br /><br />http://www.scribd.com/doc/267405/-Java-NIO-with-Grizzly
          <br/>
          <span style="color:red;">
            <a href="http://simohayha.javaeye.com/blog/176578#comments" style="color:red;">本文的讨论也很精彩，浏览讨论>></a>
          </span>
          <br/><br/><br/>
          <span style="color:#E28822;">JavaEye推荐</span>
          <br/>
          <ul class='adverts'><li><a href='/adverts/92' target='_blank'><span style="color:red;font-weight:bold;">快来参加7月17日在成都举行的SOA中国技术论坛</span></a></li><li><a href='/adverts/106' target='_blank'><span style="color:blue;font-weight:bold;">JavaEye问答大赛开始了！ 从6月23日 至 7月6日，奖品丰厚 ！</span></a></li><li><a href='/adverts/42' target='_blank'><span style="color:red;font-weight:bold;">搜狐网站诚聘Java、PHP和C++工程师</span></a></li><li><a href='/adverts/97' target='_blank'><span style="color:blue;font-weight:bold;">Oracle专区上线，有Oracle最新文章，重要下载及知识库等精彩内容，欢迎访问。</span></a></li><li><a href='/adverts/41' target='_blank'><span style="color:red;font-weight:bold;">北京: 千橡集团暨校内网诚聘软件研发工程师</span></a></li></ul>
          <br/><br/><br/>
          ]]>
        </description>
        <pubDate>Thu, 27 Mar 2008 00:40:31 +0800</pubDate>
        <link>http://simohayha.javaeye.com/blog/176578</link>
        <guid>http://simohayha.javaeye.com/blog/176578</guid>
      </item>
      <item>
        <title>大脑缺氧</title>
        <author>simohayha</author>
        <description>
          <![CDATA[
          <br/>
          作者: <a href="http://simohayha.javaeye.com">simohayha</a>&nbsp;
          链接：<a href="http://simohayha.javaeye.com/blog/175661" style="color:red;">http://simohayha.javaeye.com/blog/175661</a>&nbsp;
          发表时间: 2008年03月25日
          <br/><br/>
          声明：本文系JavaEye网站发布的原创博客文章，未经作者书面许可，严禁任何网站转载本文，否则必将追究法律责任！
          <br/><br/>
          离职手续办的真慢，没办法的说，不过下周开始就不需要上班了，最近发觉脑袋一直很沉，看书也看不进去，昨晚丁家猫同学告诉我，我才知道是大脑缺氧，我那边的工作环境就不说了，太封闭了，窗户都打不开，简直就和棺材一样，自己晚上睡得也比较晚。而且也有3，4年都没锻炼过了，准备周末去打打球，不然工作什么的效率太低了。。
          <br/>
          <span style="color:red;">
            <a href="http://simohayha.javaeye.com/blog/175661#comments" style="color:red;">本文的讨论也很精彩，浏览讨论>></a>
          </span>
          <br/><br/><br/>
          <span style="color:#E28822;">JavaEye推荐</span>
          <br/>
          <ul class='adverts'><li><a href='/adverts/97' target='_blank'><span style="color:blue;font-weight:bold;">Oracle专区上线，有Oracle最新文章，重要下载及知识库等精彩内容，欢迎访问。</span></a></li><li><a href='/adverts/106' target='_blank'><span style="color:blue;font-weight:bold;">JavaEye问答大赛开始了！ 从6月23日 至 7月6日，奖品丰厚 ！</span></a></li><li><a href='/adverts/92' target='_blank'><span style="color:red;font-weight:bold;">快来参加7月17日在成都举行的SOA中国技术论坛</span></a></li><li><a href='/adverts/41' target='_blank'><span style="color:red;font-weight:bold;">北京: 千橡集团暨校内网诚聘软件研发工程师</span></a></li><li><a href='/adverts/42' target='_blank'><span style="color:red;font-weight:bold;">搜狐网站诚聘Java、PHP和C++工程师</span></a></li></ul>
          <br/><br/><br/>
          ]]>
        </description>
        <pubDate>Tue, 25 Mar 2008 09:47:04 +0800</pubDate>
        <link>http://simohayha.javaeye.com/blog/175661</link>
        <guid>http://simohayha.javaeye.com/blog/175661</guid>
      </item>
      <item>
        <title>一些胡思乱想</title>
        <author>simohayha</author>
        <description>
          <![CDATA[
          <br/>
          作者: <a href="http://simohayha.javaeye.com">simohayha</a>&nbsp;
          链接：<a href="http://simohayha.javaeye.com/blog/173915" style="color:red;">http://simohayha.javaeye.com/blog/173915</a>&nbsp;
          发表时间: 2008年03月19日
          <br/><br/>
          声明：本文系JavaEye网站发布的原创博客文章，未经作者书面许可，严禁任何网站转载本文，否则必将追究法律责任！
          <br/><br/>
          比如很简单的一个斐波那契数列，如果纯粹从数学上来说，那就是<br /><br /><div class="quote_title">引用</div><div class="quote_div">f(n)=f(n-1)+f(n-2)当n>1时，而n=0时f(n)=0,当n=1时，f(n)=1</div><br /><br />很容易我们就可以直接翻译为程序:<br /><br /><pre name="code" class="java">(define (f n)
  (cond ((= n  0) 0)
            ((= n 1) 1)
            (else (+ (f (- n 1)) (f (- n 2))))))</pre><br /><br />这就是数学公式直接翻译为程序。从数学的角度来看，这个程序很完美，和上面的数学公式一模一样。可是从计算机角度来看这个程序非常的不好，那是因为会做很多重复的计算。<br /><br /><br />自己在这里胡思乱想一下，这是因为，写数学公式的时候并不需要考虑他是如何计算，也就是说人脑所需要做的那一部分，在公式里面并不需要考虑，而如何计算不需要公式来考虑。<br /><br />而在计算机来说，程序相当于公式，而人脑这一部分则由编译器来代替，因此这里我们写程序的时候就必须要考虑编译器怎样计算这个过程，因此在sicp里面提到了"灵巧编译器"也就是说能将上面的程序也就是树形递归过程翻译为一个能计算出同样结果的更有效的过程.(具体实现也就是构造出一个表格来保存计算出的结果，从而不必要重复计算).<br /><br />PS：一些胡思乱想，大家见笑了...
          <br/>
          <span style="color:red;">
            <a href="http://simohayha.javaeye.com/blog/173915#comments" style="color:red;">本文的讨论也很精彩，浏览讨论>></a>
          </span>
          <br/><br/><br/>
          <span style="color:#E28822;">JavaEye推荐</span>
          <br/>
          <ul class='adverts'><li><a href='/adverts/106' target='_blank'><span style="color:blue;font-weight:bold;">JavaEye问答大赛开始了！ 从6月23日 至 7月6日，奖品丰厚 ！</span></a></li><li><a href='/adverts/42' target='_blank'><span style="color:red;font-weight:bold;">搜狐网站诚聘Java、PHP和C++工程师</span></a></li><li><a href='/adverts/41' target='_blank'><span style="color:red;font-weight:bold;">北京: 千橡集团暨校内网诚聘软件研发工程师</span></a></li><li><a href='/adverts/92' target='_blank'><span style="color:red;font-weight:bold;">快来参加7月17日在成都举行的SOA中国技术论坛</span></a></li><li><a href='/adverts/97' target='_blank'><span style="color:blue;font-weight:bold;">Oracle专区上线，有Oracle最新文章，重要下载及知识库等精彩内容，欢迎访问。</span></a></li></ul>
          <br/><br/><br/>
          ]]>
        </description>
        <pubDate>Wed, 19 Mar 2008 22:41:01 +0800</pubDate>
        <link>http://simohayha.javaeye.com/blog/173915</link>
        <guid>http://simohayha.javaeye.com/blog/173915</guid>
      </item>
      <item>
        <title>一些流行的书的缩写</title>
        <author>simohayha</author>
        <description>
          <![CDATA[
          <br/>
          作者: <a href="http://simohayha.javaeye.com">simohayha</a>&nbsp;
          链接：<a href="http://simohayha.javaeye.com/blog/173735" style="color:red;">http://simohayha.javaeye.com/blog/173735</a>&nbsp;
          发表时间: 2008年03月19日
          <br/><br/>
          声明：本文系JavaEye网站发布的原创博客文章，未经作者书面许可，严禁任何网站转载本文，否则必将追究法律责任！
          <br/><br/>
          PS:最近都没怎么写blog，主要是忙着离职手续，和手头上的活的交接。<br /><br /><br />这个是在reddit看到的，就是一些很好的书的缩写：<br /><br /><div class="quote_title">引用</div><div class="quote_div">AIMA = Artificial Intelligence a Modern Approach by Stuart Russell and Peter Norvig<br /><br />AMOP = The Art of the Meta Object Protocol by Gregor Kiczales<br /><br />ATTAPL = Advanced Topics in Types and Programming Languages by Benjamin C. Pierce<br /><br />AWDWR = Agile Web Development with Rails by by Dave Thomas and David Heinemeier Hansson<br /><br />EOPL = Essentials of Programming Languages by Daniel P. Friedman, Mitchell Wand, and Christopher T. Haynes<br /><br />CLR = Introduction to Algorithms by Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest<br /><br />CLRS = Introduction to Algorithms, 2nd Edition by Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest, and Cliff Stein<br /><br />CLtL = Common Lisp the Language by Guy L. Steele Jr.<br /><br />CTM = Concepts, Techniques, and Models of Computer Programming by Peter Van Roy and Seif Haridi<br /><br />GOF = Design Patterns by Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides<br /><br />HTDP = How To Design Programs by Matthias Felleisen, Robert Bruce Findler, Matthew Flatt and Shriram Krishnamurthi<br /><br />K&R = The C Programming Language by Brian Kernighan and Dennis Ritchie<br /><br />LiSP = Lisp In Small Pieces by Christian Queinnec<br /><br />LL = Lessons Learned in Software Testing by Cem Kaner, James Bach and Bret Pettichord<br /><br />PAIP = Paradigms of Artificial Intelligence Programming by Peter Norvig<br /><br />PCL = Practical Common Lisp by Peter Seibel<br /><br />PLP = Programming Language Pragmatics by Michael L. Scott<br /><br />SICP = Structure and Interpretation of Computer Programs by Hal Abelson and Gerald J. Sussman, with Julie Sussman<br /><br />TAOCP = The Art Of Computer Programming by Donald E. Knuth<br /><br />TAPL = Types And Programming Languages by Benjamin C. Pierce<br /><br />TCPL = The C++ Programming Language by Bjarne Stroustrup<br /><br />TCS = Testing Computer Software by Cem Kaner, Jack Falk, and Hung Quoc Nguyen<br /><br />TLS = The Little Schemer by Daniel P. Friedman and Matthias Felleisen<br /><br />TSPL = The Scheme Programming Language by R. Kent Dybvig<br /><br />TSS = The Seasoned Schemer by Daniel P. Friedman and Matthias Felleisen<br /><br />TRS = The Reasoned Schemer by Daniel P. Friedman, William E. Byrd and Oleg Kiselyov<br /><br />WELC = Working Effectively with Legacy Code by Michael Feathers</div>
          <br/>
          <span style="color:red;">
            <a href="http://simohayha.javaeye.com/blog/173735#comments" style="color:red;">本文的讨论也很精彩，浏览讨论>></a>
          </span>
          <br/><br/><br/>
          <span style="color:#E28822;">JavaEye推荐</span>
          <br/>
          <ul class='adverts'><li><a href='/adverts/92' target='_blank'><span style="color:red;font-weight:bold;">快来参加7月17日在成都举行的SOA中国技术论坛</span></a></li><li><a href='/adverts/41' target='_blank'><span style="color:red;font-weight:bold;">北京: 千橡集团暨校内网诚聘软件研发工程师</span></a></li><li><a href='/adverts/42' target='_blank'><span style="color:red;font-weight:bold;">搜狐网站诚聘Java、PHP和C++工程师</span></a></li><li><a href='/adverts/106' target='_blank'><span style="color:blue;font-weight:bold;">JavaEye问答大赛开始了！ 从6月23日 至 7月6日，奖品丰厚 ！</span></a></li><li><a href='/adverts/97' target='_blank'><span style="color:blue;font-weight:bold;">Oracle专区上线，有Oracle最新文章，重要下载及知识库等精彩内容，欢迎访问。</span></a></li></ul>
          <br/><br/><br/>
          ]]>
        </description>
        <pubDate>Wed, 19 Mar 2008 13:55:31 +0800</pubDate>
        <link>http://simohayha.javaeye.com/blog/173735</link>
        <guid>http://simohayha.javaeye.com/blog/173735</guid>
      </item>
      <item>
        <title>[转帖]算法之美</title>
        <author>simohayha</author>
        <description>
          <![CDATA[
          <br/>
          作者: <a href="http://simohayha.javaeye.com">simohayha</a>&nbsp;
          链接：<a href="http://simohayha.javaeye.com/blog/171513" style="color:red;">http://simohayha.javaeye.com/blog/171513</a>&nbsp;
          发表时间: 2008年03月14日
          <br/><br/>
          声明：本文系JavaEye网站发布的原创博客文章，未经作者书面许可，严禁任何网站转载本文，否则必将追究法律责任！
          <br/><br/>
          原文在这里;<br /><br />http://www.douban.com/review/1325850/<br /><br />里面说的是这本书<br />http://www.amazon.com/o/ASIN/0073523402/102-9052822-5114543?SubscriptionId=1100889MK2XY9PSTV5G2<br /><div class="quote_title">引用</div><div class="quote_div"><br />这是本很新的书，06年末发行，07年才慢慢出现于人们的视野。我在08年初得知这本书，那会我还很奇怪：都什么年月了，怎么还有人写算法教材——这么“经典”的工作，不是上个世纪就被人做完了吗。 <br />　　 <br />　　读了这本Algorithms，我才知道：这才是我心中的算法书，我等待这样一本书已经很多年了。它的确当得起这个名字。 <br />　　 <br />　　书的三位作者：Sanjoy Dasgupta, Papadimitriou, Umesh Vazirani。 <br />　　 <br />　　其中，Umesh堪称计算机理论界的第二名师（第一名师是他自己的导师Manuel Blum），他带过的学生们犹如一个理论计算机科学新生代的全明星队。另一个作者Papadimitriou可算是理论界的第二名笔（第一非Knuth莫属），他的书Computational Complexity和Combinatorial optimization堪称理论计算机科学最好读的专业书，他业余还写了本小说"Turing"。第三个作者Dasgupta是个算法方向的研究者，他最年轻，本身就是Umesh的学生，相比前面二位也没什么噱头——可他注定要因这本Algorithms而被载入计算机科学的史册。 <br />　　 <br />　　在这本书之前，算法的经典教材首推CLRS的算法导论。算法导论让人印象深刻的，是它内容的全面翔实，还有它一千两百页的厚度。 <br />　　 <br />　　而见到这本Algorithms时，你会震惊于它的薄。我从亚马逊收到这本书时，还以为拿错了包裹。 <br />　　 <br />　　可读过之后，你就会折服于它的美。 <br />　　 <br />　　这是一本可以给人带来巨大阅读乐趣的专业书籍。作者娓娓道来，又惜墨如金。用极精炼的语言，为我们指明了一条通向那些美丽算法的线索。我要由衷地说：这本书不仅仅是一些结果的集合，更是一段美好的旅程。我对书中涉及的内容已然熟悉，但读过之后仍感收获良多，对算法这门学问又多了些认识。真的是，写书当如是。 <br />　　 <br />　　 <br />　　对我来说，算法的教与学有两个困难的地方： <br />　　 <br />　　其一，我们学习了那些经典的算法，除了赞叹一下设计的巧思，但总难免问上一句：怎么想到的？对学生来说，这可能是最费解、也最让人窝火的地方。我们下再多的功夫去记忆书上的算法、去分析这些算法的效率，却终究不能理喻得到这些算法的过程。心理盘算着：给我一个新问题，让我设计个算法出来，我能行吗？答案是：不知道。 <br />　　 <br />　　可这偏偏又是极重要的，无论作研究还是实际工作，一个计算机专业人士最重要的能力，就是解决问题——解决那些不断从理论模型、或者从实际应用中冒出来的新问题。 <br />　　 <br />　　其二，算法作为一门学问，有两条正交的线索。一个是算法处理的对象：数、矩阵、集合、串(strings)、排列(permutations)、图(graphs)、表达式(formula)、分布(distributions)，等等。另一个是算法的设计思想：贪婪、分治、动态规划、线性规划、局部搜索(local search)，等等。这两条线索几乎是相互独立的：同一个离散对象，例如图，稍有不同的问题，例如single-source shortest path和all-pair shortest path，就可以用到不同的设计思想，如贪婪和动态规划；而完全不同的离散对象上的问题，例如排序和整数乘法，也许就会用到相同的思想，例如分治。 <br />　　 <br />　　两条线索交织在一起，该如何表述。对学生而言，不同离散对象的差别是直观的——我们已经惯于在一章里面完全讲排序、而在另一章里面完全讲图论算法；可是对算法而言，设计思想的差别是根本的，因为这是从问题的结构来的：不同离散对象上面定义的问题，可以展现出类似的结构，而这结构特征，就是支持一类算法的根本，也是我们设计算法的依据。 <br />　　 <br />　　 <br />　　坦率的说，之前还没有哪一本算法书很好的解决这两个困难，就连算法导论在这两个问题上也都做得不好。传统的算法书，大多注重内容(content)的收录，但却忽视思维过程的展示(exposition)，因此我们学习了经典的算法，却费解于得到算法的过程；而且算法教材对于内容的编排多是枚举式的(enumerative)，这多少是受了the art of computer programming的影响——可那是本工具书而不是教材，因此我们一提到算法课，就想起了排序、哈希、最短路径……这些题目(topics)，却没有一个统一的线索在心中。 <br />　　 <br />　　这本Algorithms，在短短的篇幅内，做到了。 <br />　　 <br />　　三位作者可谓野心勃勃，几乎是胆大妄为。他们对传统算法教学思路的颠覆和背叛可谓前所未有。刚拿到目录的时候，我就替他们捏了一把汗，觉得这哪里像一本“正经”的算法书。可读下来，却不由得佩服——算法书早该这么写了。 <br />　　 <br />　　他们并没有要全面的收录各种各样的算法，他们做的事情是理清了一条算法这门学问的线索。因此填鸭式的内容灌输不是这本书的目的；对结构的精心安排，对问题的数学结构的剖析、从而推出一个算法的过程的讲解，都体现除了这本书真正的用心：它要让学生获得最大程度的启发，要训练学生独立解决问题的能力。 <br />　　 <br />　　我觉得这才是教育真正的目的，也是算法课应该追求的目标。 <br />　　 <br />　　 <br />　　说完了种种溢美之词，也来补充一下这本书的不足。这样一本精炼的算法书，为了它道理的清晰、为了它的美，必然会放弃一点对面面俱到的追求。如果我用这本书来教一门算法课的话，我会增加一点以下的内容： <br />　　 <br />　　1. 数据结构。 <br />　　这本书对数据结构没有单独的章节，都是在某个数据结构被一个算法用到的时候讲一下，例如priority queue之于Dijkstra's algorithm。这种做法体现了作者的观点：这门课完全就是关于algorithms，数据结构对于算法而言就只是个工具。如果同一个系还能开出一门很强的data structures课，这么做当然很好，各有侧重。但若是我来上课，肯定会提一下数据结构的一些重要思想，例如hashing，和他们的数学背景。因为对于一些实际问题，数据结构已不再是个工具，可能就是问题本身。 <br />　　 <br />　　2. 几个没有被此书涉及到的算法设计和分析的工具：对手论证(adversarial argument)，matroid，平摊分析(amortized analysis)。 <br />　　 <br />　　3. 书中每个算法问题目前最好的上下界(upper bounds, lower bounds)。 <br />　　对于一本书而言，让它记录这些不太现实，因为除非上下界已经紧了，也许出版的第二天就会有更好的上界或下界（其实这事已经发生了，书最结尾historical notes提了一句整数乘法的fastest known algorithm，结果现在这个结果已经被刷新了）。但老师上课的时候，应该跟学生们讲一下这个内容，让学生心里有这个“上下界”和"open problem"的概念，也晓得算法不是死知识，而是正在发生中的事。 <br />　　 <br />　　4. 讲一点比贪婪、动态规划等等更加“现代”的算法设计的思想，例如spectral analysis, metric embedding, rapid mixing of markov chain等等。 <br />　　也提一点当下的实际问题（例如google或豆瓣想解决的问题）面临的一些新的考虑，例如非经典的现实的计算模型：考虑内外存的I/O模型，面对海量数据输入的streaming model，海量数据的sub-linear algorithms等等。这些现实模型上的算法需要重新设计去面对新的考量。 <br />　　我理解Algorithms这本书没有收录这些内容的原因。越是新的东西老的越快，没有人希望自己的书很快过时。但上课不妨出一些这样的case studies，时髦的东西学生肯定会很感兴趣，这些新东西的粗糙也可以锻炼学生实际解决问题的能力。 <br />　　 <br />　　5. 除了这本Algorithms作为教材，补充读物可以在CLRS算法导论和Kleinberg和Tardos的算法设计（这也是本顶新的书）之间选择一本。我个人推荐后者。</div><br /><br />后来又去看了这个作者对算法导论的评价，里面有段也说的挺好的：<br /><br />http://www.douban.com/review/1319527/<br /><br /><div class="quote_title">引用</div><div class="quote_div">对于算法的伪码描述，倒不必太仔细了。不能指望在算法课上学习编程，算法本来就是很纯粹的数学对象，它的设计思想完全依托于背后的数学结构，它运作的机制以及它的美，也都来自它的数学，可是书上那些模仿C和Pascal的语句，让算法的数学之美沦为一段机械代码。读者辛苦的把自己的思维变成机器，读懂了这些代码，但并不会直接带来对算法本身的领悟。就像一个人懂得了打牌的游戏规则，但并不意味着他就会打牌了，因为他可能依旧不通晓牌理。对算法的学习也要从问题本身的数学结构入手，理解解决此种结构问题的算法它的设计思想，掌握分析具有各种结构特征的算法的数学工具，学习怎样发现问题的结构并从中推出问题的下界（lower bound）。这些才是学习算法的根本。</div>
          <br/>
          <span style="color:red;">
            <a href="http://simohayha.javaeye.com/blog/171513#comments" style="color:red;">本文的讨论也很精彩，浏览讨论>></a>
          </span>
          <br/><br/><br/>
          <span style="color:#E28822;">JavaEye推荐</span>
          <br/>
          <ul class='adverts'><li><a href='/adverts/97' target='_blank'><span style="color:blue;font-weight:bold;">Oracle专区上线，有Oracle最新文章，重要下载及知识库等精彩内容，欢迎访问。</span></a></li><li><a href='/adverts/41' target='_blank'><span style="color:red;font-weight:bold;">北京: 千橡集团暨校内网诚聘软件研发工程师</span></a></li><li><a href='/adverts/106' target='_blank'><span style="color:blue;font-weight:bold;">JavaEye问答大赛开始了！ 从6月23日 至 7月6日，奖品丰厚 ！</span></a></li><li><a href='/adverts/42' target='_blank'><span style="color:red;font-weight:bold;">搜狐网站诚聘Java、PHP和C++工程师</span></a></li><li><a href='/adverts/92' target='_blank'><span style="color:red;font-weight:bold;">快来参加7月17日在成都举行的SOA中国技术论坛</span></a></li></ul>
          <br/><br/><br/>
          ]]>
        </description>
        <pubDate>Fri, 14 Mar 2008 21:37:27 +0800</pubDate>
        <link>http://simohayha.javaeye.com/blog/171513</link>
        <guid>http://simohayha.javaeye.com/blog/171513</guid>
      </item>
      <item>
        <title>工作的事情</title>
        <author>simohayha</author>
        <description>
          <![CDATA[
          <br/>
          作者: <a href="http://simohayha.javaeye.com">simohayha</a>&nbsp;
          链接：<a href="http://simohayha.javaeye.com/blog/170961" style="color:red;">http://simohayha.javaeye.com/blog/170961</a>&nbsp;
          发表时间: 2008年03月13日
          <br/><br/>
          声明：本文系JavaEye网站发布的原创博客文章，未经作者书面许可，严禁任何网站转载本文，否则必将追究法律责任！
          <br/><br/>
          工作的事情终于告一段落了，就要去广州了，这边厦门的离职手续正在办，希望能早点办完，其实也不能说现在的公司怎么怎么不好，只能说不适合自己吧，虽然和项目组的兄弟们呆的时间不长，只有1年，可是彼此之间还是很投机的，自己有时说话不对的，这里给兄弟们道歉了。<br /><br />这次找工作，要多谢dennis_zane同学了，我也说不出啥感谢的话，一切尽在不言中了。<br /><br />说起来很奇怪，记得当年我是最不想去的城市就是广州了，没想到自己反而去了广州，真是造化弄人，无数个路口，无数个结果，不知道这次会发生什么事情.
          <br/>
          <span style="color:red;">
            <a href="http://simohayha.javaeye.com/blog/170961#comments" style="color:red;">本文的讨论也很精彩，浏览讨论>></a>
          </span>
          <br/><br/><br/>
          <span style="color:#E28822;">JavaEye推荐</span>
          <br/>
          <ul class='adverts'><li><a href='/adverts/42' target='_blank'><span style="color:red;font-weight:bold;">搜狐网站诚聘Java、PHP和C++工程师</span></a></li><li><a href='/adverts/106' target='_blank'><span style="color:blue;font-weight:bold;">JavaEye问答大赛开始了！ 从6月23日 至 7月6日，奖品丰厚 ！</span></a></li><li><a href='/adverts/92' target='_blank'><span style="color:red;font-weight:bold;">快来参加7月17日在成都举行的SOA中国技术论坛</span></a></li><li><a href='/adverts/41' target='_blank'><span style="color:red;font-weight:bold;">北京: 千橡集团暨校内网诚聘软件研发工程师</span></a></li><li><a href='/adverts/97' target='_blank'><span style="color:blue;font-weight:bold;">Oracle专区上线，有Oracle最新文章，重要下载及知识库等精彩内容，欢迎访问。</span></a></li></ul>
          <br/><br/><br/>
          ]]>
        </description>
        <pubDate>Thu, 13 Mar 2008 21:17:30 +0800</pubDate>
        <link>http://simohayha.javaeye.com/blog/170961</link>
        <guid>http://simohayha.javaeye.com/blog/170961</guid>
      </item>
      <item>
        <title>又一个失望的夜晚</title>
        <author>simohayha</author>
        <description>
          <![CDATA[
          <br/>
          作者: <a href="http://simohayha.javaeye.com">simohayha</a>&nbsp;
          链接：<a href="http://simohayha.javaeye.com/blog/170341" style="color:red;">http://simohayha.javaeye.com/blog/170341</a>&nbsp;
          发表时间: 2008年03月12日
          <br/><br/>
          声明：本文系JavaEye网站发布的原创博客文章，未经作者书面许可，严禁任何网站转载本文，否则必将追究法律责任！
          <br/><br/>
          5点打开pplive，落后0:1,我就知道没什么戏了，每年都期望欧冠有突破，每年都让人那么失望，不过运气也太不好了，一到关键时候，受伤的受伤，没状态的没状态。不过没关系，明年再来，不过中场我觉得老爹改买几个人了，阿奎拉尼，迪亚拉，夸雷斯马，我觉得都不错，维爱国还是卖了吧。<br /><br />馒头决定要辞职了，虽然我是个坚定的倒曼者，不过还是祝福他，一路走好，毕竟还是带给过我们很多欢乐。<br /><br />http://www.uefa.com/competitions/ucl/news/kind=1/newsid=671079.html<br /><br /><div class="quote_title">引用</div><div class="quote_div">The 43-year-old broke the news in the conference which followed Inter's 1-0 home defeat by Liverpool FC, a loss which ended their UEFA Champions League participation in the first knockout round once more. Mancini, a former Italy striker, said: "<span style="color: red"><span style="font-size: medium">The next two and half months will be my last in charge of Inter. I'd already told the boys and it's only right to let everybody know. I'd already made this decision and it doesn't depend on tonight's defeat</span></span>."</div>
          <br/>
          <span style="color:red;">
            <a href="http://simohayha.javaeye.com/blog/170341#comments" style="color:red;">本文的讨论也很精彩，浏览讨论>></a>
          </span>
          <br/><br/><br/>
          <span style="color:#E28822;">JavaEye推荐</span>
          <br/>
          <ul class='adverts'><li><a href='/adverts/106' target='_blank'><span style="color:blue;font-weight:bold;">JavaEye问答大赛开始了！ 从6月23日 至 7月6日，奖品丰厚 ！</span></a></li><li><a href='/adverts/42' target='_blank'><span style="color:red;font-weight:bold;">搜狐网站诚聘Java、PHP和C++工程师</span></a></li><li><a href='/adverts/41' target='_blank'><span style="color:red;font-weight:bold;">北京: 千橡集团暨校内网诚聘软件研发工程师</span></a></li><li><a href='/adverts/97' target='_blank'><span style="color:blue;font-weight:bold;">Oracle专区上线，有Oracle最新文章，重要下载及知识库等精彩内容，欢迎访问。</span></a></li><li><a href='/adverts/92' target='_blank'><span style="color:red;font-weight:bold;">快来参加7月17日在成都举行的SOA中国技术论坛</span></a></li></ul>
          <br/><br/><br/>
          ]]>
        </description>
        <pubDate>Wed, 12 Mar 2008 08:43:39 +0800</pubDate>
        <link>http://simohayha.javaeye.com/blog/170341</link>
        <guid>http://simohayha.javaeye.com/blog/170341</guid>
      </item>
      <item>
        <title>自勉一下</title>
        <author>simohayha</author>
        <description>
          <![CDATA[
          <br/>
          作者: <a href="http://simohayha.javaeye.com">simohayha</a>&nbsp;
          链接：<a href="http://simohayha.javaeye.com/blog/169548" style="color:red;">http://simohayha.javaeye.com/blog/169548</a>&nbsp;
          发表时间: 2008年03月09日
          <br/><br/>
          声明：本文系JavaEye网站发布的原创博客文章，未经作者书面许可，严禁任何网站转载本文，否则必将追究法律责任！
          <br/><br/>
          这是一个好朋友送我的，最近工作的事弄得有点烦，拿出来再次自勉一下：<br /><br /><div class="quote_title">引用</div><div class="quote_div">心如规矩，志如尺衡，平静如水，正直如绳。</div>
          <br/>
          <span style="color:red;">
            <a href="http://simohayha.javaeye.com/blog/169548#comments" style="color:red;">本文的讨论也很精彩，浏览讨论>></a>
          </span>
          <br/><br/><br/>
          <span style="color:#E28822;">JavaEye推荐</span>
          <br/>
          <ul class='adverts'><li><a href='/adverts/41' target='_blank'><span style="color:red;font-weight:bold;">北京: 千橡集团暨校内网诚聘软件研发工程师</span></a></li><li><a href='/adverts/106' target='_blank'><span style="color:blue;font-weight:bold;">JavaEye问答大赛开始了！ 从6月23日 至 7月6日，奖品丰厚 ！</span></a></li><li><a href='/adverts/42' target='_blank'><span style="color:red;font-weight:bold;">搜狐网站诚聘Java、PHP和C++工程师</span></a></li><li><a href='/adverts/97' target='_blank'><span style="color:blue;font-weight:bold;">Oracle专区上线，有Oracle最新文章，重要下载及知识库等精彩内容，欢迎访问。</span></a></li><li><a href='/adverts/92' target='_blank'><span style="color:red;font-weight:bold;">快来参加7月17日在成都举行的SOA中国技术论坛</span></a></li></ul>
          <br/><br/><br/>
          ]]>
        </description>
        <pubDate>Sun, 09 Mar 2008 19:20:49 +0800</pubDate>
        <link>http://simohayha.javaeye.com/blog/169548</link>
        <guid>http://simohayha.javaeye.com/blog/169548</guid>
      </item>
      <item>
        <title>发现了一个好东西LLVM</title>
        <author>simohayha</author>
        <description>
          <![CDATA[
          <br/>
          作者: <a href="http://simohayha.javaeye.com">simohayha</a>&nbsp;
          链接：<a href="http://simohayha.javaeye.com/blog/169011" style="color:red;">http://simohayha.javaeye.com/blog/169011</a>&nbsp;
          发表时间: 2008年03月07日
          <br/><br/>
          声明：本文系JavaEye网站发布的原创博客文章，未经作者书面许可，严禁任何网站转载本文，否则必将追究法律责任！
          <br/><br/>
          LLVM全称是 Low Level Virtual Machine<br /><br /><div class="quote_title">引用</div><div class="quote_div">LLVM is a robust system, particularly well suited for developing new mid-level language-independent analyses and optimizations of all sorts, including those that require extensive interprocedural analysis. LLVM is also a great target for front-end development for conventional or research programming languages, including those which require compile-time, link-time, or run-time optimization for effective implementation, proper tail calls or garbage collection</div><br /><br /><br />主页在这里<br /><br />http://llvm.org/<br /><br /><br /><br />这里有个介绍的PPT:<br /><br />http://llvm.cs.uiuc.​edu/pubs/2007-03-12-​BossaLLVMIntro.pdf<br /><br /><br />可惜自己编译的知识太匮乏了。。准备好好补补，然后再研究研究这个..
          <br/>
          <span style="color:red;">
            <a href="http://simohayha.javaeye.com/blog/169011#comments" style="color:red;">本文的讨论也很精彩，浏览讨论>></a>
          </span>
          <br/><br/><br/>
          <span style="color:#E28822;">JavaEye推荐</span>
          <br/>
          <ul class='adverts'><li><a href='/adverts/106' target='_blank'><span style="color:blue;font-weight:bold;">JavaEye问答大赛开始了！ 从6月23日 至 7月6日，奖品丰厚 ！</span></a></li><li><a href='/adverts/97' target='_blank'><span style="color:blue;font-weight:bold;">Oracle专区上线，有Oracle最新文章，重要下载及知识库等精彩内容，欢迎访问。</span></a></li><li><a href='/adverts/42' target='_blank'><span style="color:red;font-weight:bold;">搜狐网站诚聘Java、PHP和C++工程师</span></a></li><li><a href='/adverts/41' target='_blank'><span style="color:red;font-weight:bold;">北京: 千橡集团暨校内网诚聘软件研发工程师</span></a></li><li><a href='/adverts/92' target='_blank'><span style="color:red;font-weight:bold;">快来参加7月17日在成都举行的SOA中国技术论坛</span></a></li></ul>
          <br/><br/><br/>
          ]]>
        </description>
        <pubDate>Fri, 07 Mar 2008 11:56:25 +0800</pubDate>
        <link>http://simohayha.javaeye.com/blog/169011</link>
        <guid>http://simohayha.javaeye.com/blog/169011</guid>
      </item>
      <item>
        <title>ruby1.9中的Iterators</title>
        <author>simohayha</author>
        <description>
          <![CDATA[
          <br/>
          作者: <a href="http://simohayha.javaeye.com">simohayha</a>&nbsp;
          链接：<a href="http://simohayha.javaeye.com/blog/168483" style="color:red;">http://simohayha.javaeye.com/blog/168483</a>&nbsp;
          发表时间: 2008年03月05日
          <br/><br/>
          声明：本文系JavaEye网站发布的原创博客文章，未经作者书面许可，严禁任何网站转载本文，否则必将追究法律责任！
          <br/><br/>
          在ruby1.9中增加了External Iterators这中新的迭代类型，所有的Enumerable 都是External Iterators.而在ruby1.9之前的版本中的迭代都是internal iterator。<br /><br />何为external iterator呢，其实我认为也就是一个生成器了：<br /><br /><pre name="code" class="ruby">iterator = 9.downto(1)             # An enumerator as external iterator
begin                              # So we can use rescue below
  print iterator.next while true   # Call the next method repeatedly
rescue StopIteration               # When there are no more values
  puts "...blastoff!"              # An expected, nonexceptional condition
end</pre><br /><br /><br /><br />这里的Kernel.loop方法就是为了迭代External Iterators而专门创建的。当next方法到最后一个元素的时候，就会抛出StopIteration，因此这里我们捕捉这个异常来判断迭代是否结束.<br /><br />任何External Iterators都可以调用rewind 方法来重启这个迭代器。要注意如果一个enumerator 基于像File这样的对象，那么调用rewind将不会起作用。<br /><br />一旦一个External Iterators开始迭代，那么他就不能被clone和duplicated。<br /><br />我们可以简单的使用External Iterators来创建一个each方法，也就是通过External Iterator来创建一个internal iterator：<br /><br /><pre name="code" class="ruby">module Iterable
  include Enumerable          # Define iterators on top of each
  def each                    # And define each on top of next
    loop { yield self.next }
  end
end</pre><br /><br />下面我们可以这样做：<br /><br /><pre name="code" class="ruby">def iterate(iterator)
  loop { yield iterator.next }
end

iterate(9.downto(1)) {|x| print x }</pre><br /><br /><br />下面是三个函数，功能都是一样的，第一个使用internal iterators,第二个虽然使用了External iterators ,可是没有用loop方法，因此代码很丑陋，第三个方法是最好的方法。：<br /><br /><pre name="code" class="ruby"># Call the each method of each collection in turn.
# This is not a parallel iteration and does not require enumerators.
def sequence(*enumerables, &block)
  enumerables.each do |enumerable|
    enumerable.each(&block)
  end
end

# Iterate the specified collections, interleaving their elements.
# This can't be done efficiently without external iterators.
# Note the use of the uncommon else clause in begin/rescue.
def interleave(*enumerables)
  # Convert enumerable collections to an array of enumerators.
  enumerators = enumerables.map {|e| e.to_enum }
  # Loop until we don't have any more enumerators.
  until enumerators.empty?
    begin
      e = enumerators.shift   # Take the first enumerator
      yield e.next            # Get its next and pass to the block
    rescue StopIteration      # If no more elements, do nothing
    else                      # If no exception occurred
      enumerators &lt;&lt; e        # Put the enumerator back
    end
  end
end

# Iterate the specified collections, yielding tuples of values,
# one value from each of the collections. See also Enumerable.zip.
def bundle(*enumerables)
  enumerators = enumerables.map {|e| e.to_enum }
  loop { yield enumerators.map {|e| e.next} }
end

# Examples of how these iterator methods work
a,b,c = [1,2,3], 4..6, 'a'..'e'
sequence(a,b,c) {|x| print x}   # prints "123456abcde"
interleave(a,b,c) {|x| print x} # prints "14a25b36cde"
bundle(a,b,c) {|x| print x}     # '[1, 4, "a"][2, 5, "b"][3, 6, "c"]'</pre><br /><br />这里要注意，在ruby1.8中的zip效率很低，那是因为它首先转换参数为数组，然后迭代他，可是在1.9中，由于我们有了External Iterators,因此它的实现就是直接迭代。因此他就更高效.
          <br/>
          <span style="color:red;">
            <a href="http://simohayha.javaeye.com/blog/168483#comments" style="color:red;">本文的讨论也很精彩，浏览讨论>></a>
          </span>
          <br/><br/><br/>
          <span style="color:#E28822;">JavaEye推荐</span>
          <br/>
          <ul class='adverts'><li><a href='/adverts/92' target='_blank'><span style="color:red;font-weight:bold;">快来参加7月17日在成都举行的SOA中国技术论坛</span></a></li><li><a href='/adverts/42' target='_blank'><span style="color:red;font-weight:bold;">搜狐网站诚聘Java、PHP和C++工程师</span></a></li><li><a href='/adverts/41' target='_blank'><span style="color:red;font-weight:bold;">北京: 千橡集团暨校内网诚聘软件研发工程师</span></a></li><li><a href='/adverts/97' target='_blank'><span style="color:blue;font-weight:bold;">Oracle专区上线，有Oracle最新文章，重要下载及知识库等精彩内容，欢迎访问。</span></a></li><li><a href='/adverts/106' target='_blank'><span style="color:blue;font-weight:bold;">JavaEye问答大赛开始了！ 从6月23日 至 7月6日，奖品丰厚 ！</span></a></li></ul>
          <br/><br/><br/>
          ]]>
        </description>
        <pubDate>Wed, 05 Mar 2008 22:37:03 +0800</pubDate>
        <link>http://simohayha.javaeye.com/blog/168483</link>
        <guid>http://simohayha.javaeye.com/blog/168483</guid>
      </item>
      <item>
        <title>一个简单的ruby Metaprogram的例子</title>
        <author>simohayha</author>
        <description>
          <![CDATA[
          <br/>
          作者: <a href="http://simohayha.javaeye.com">simohayha</a>&nbsp;
          链接：<a href="http://simohayha.javaeye.com/blog/167111" style="color:red;">http://simohayha.javaeye.com/blog/167111</a>&nbsp;
          发表时间: 2008年03月03日
          <br/><br/>
          声明：本文系JavaEye网站发布的原创博客文章，未经作者书面许可，严禁任何网站转载本文，否则必将追究法律责任！
          <br/><br/>
          比如下面一个文件 people.txt<br /><br /><div class="quote_title">引用</div><div class="quote_div">name,age,weight,height<br />"Smith, John", 35, 175, "5'10"<br />"Ford, Anne", 49, 142, "5'4"<br />"Taylor, Burt", 55, 173, "5'10"<br />"Zubrin, Candace", 23, 133, "5'6"</div><br /><br />我们的目标是什么呢？我们的目标就是实现类似于Rails中的Builder模块所做的事情，假设我们第二行数据 对象为a,那么我们可以通过a.name,a.age等等来得到他所对应的值:<br /><br /><pre name="code" class="java">class DataRecord    
      def self.make(file_name)
        data = File.new(file_name)
        header = data.gets.chomp
        data.close
#首先得到文件名，然后根据文件名动态的创建此文件对应的类
        class_name = File.basename(file_name,".txt").capitalize  
        klass = Object.const_set(class_name,Class.new)
        names = header.split(",")
#设置实例变量，并且将实例变量赋值
        klass.class_eval do
          attr_accessor *names
          define_method(:initialize) do |*values| 
            names.each_with_index do |name,i| 
              instance_variable_set("@"+name, values[i])
            end
          end   
#当put时能打印出显示更好的结果
          define_method(:to_s) do
            str = "&lt;#{self.class}:"
            names.each {|name| str &lt;&lt; " #{name}=#{self.send(name)}" }
            str + ">"
          end
          alias_method :inspect, :to_s
        end   
#读取数据然后将每一行数据都放进一个数组
        def klass.read
          array = []
          data = File.new(self.to_s.downcase+".txt")
          data.gets
          data.each do |line| 
            line.chomp!   
            values = eval("[#{line}]")
            array &lt;&lt; self.new(*values)
          end
          data.close
          array
        end  
        klass
      end  
end

DataRecord.make("people.txt")
list = People.read   

p list[1].name 
p list[1].age  </pre>
          <br/>
          <span style="color:red;">
            <a href="http://simohayha.javaeye.com/blog/167111#comments" style="color:red;">本文的讨论也很精彩，浏览讨论>></a>
          </span>
          <br/><br/><br/>
          <span style="color:#E28822;">JavaEye推荐</span>
          <br/>
          <ul class='adverts'><li><a href='/adverts/97' target='_blank'><span style="color:blue;font-weight:bold;">Oracle专区上线，有Oracle最新文章，重要下载及知识库等精彩内容，欢迎访问。</span></a></li><li><a href='/adverts/42' target='_blank'><span style="color:red;font-weight:bold;">搜狐网站诚聘Java、PHP和C++工程师</span></a></li><li><a href='/adverts/106' target='_blank'><span style="color:blue;font-weight:bold;">JavaEye问答大赛开始了！ 从6月23日 至 7月6日，奖品丰厚 ！</span></a></li><li><a href='/adverts/92' target='_blank'><span style="color:red;font-weight:bold;">快来参加7月17日在成都举行的SOA中国技术论坛</span></a></li><li><a href='/adverts/41' target='_blank'><span style="color:red;font-weight:bold;">北京: 千橡集团暨校内网诚聘软件研发工程师</span></a></li></ul>
          <br/><br/><br/>
          ]]>
        </description>
        <pubDate>Mon, 03 Mar 2008 23:49:06 +0800</pubDate>
        <link>http://simohayha.javaeye.com/blog/167111</link>
        <guid>http://simohayha.javaeye.com/blog/167111</guid>
      </item>
      <item>
        <title>Ruby Object Model</title>
        <author>simohayha</author>
        <description>
          <![CDATA[
          <br/>
          作者: <a href="http://simohayha.javaeye.com">simohayha</a>&nbsp;
          链接：<a href="http://simohayha.javaeye.com/blog/167057" style="color:red;">http://simohayha.javaeye.com/blog/167057</a>&nbsp;
          发表时间: 2008年03月03日
          <br/><br/>
          声明：本文系JavaEye网站发布的原创博客文章，未经作者书面许可，严禁任何网站转载本文，否则必将追究法律责任！
          <br/><br/>
          刚好看到，保存一下.
          <br/>
          <span style="color:red;">
            <a href="http://simohayha.javaeye.com/blog/167057#comments" style="color:red;">本文的讨论也很精彩，浏览讨论>></a>
          </span>
          <br/><br/><br/>
          <span style="color:#E28822;">JavaEye推荐</span>
          <br/>
          <ul class='adverts'><li><a href='/adverts/97' target='_blank'><span style="color:blue;font-weight:bold;">Oracle专区上线，有Oracle最新文章，重要下载及知识库等精彩内容，欢迎访问。</span></a></li><li><a href='/adverts/42' target='_blank'><span style="color:red;font-weight:bold;">搜狐网站诚聘Java、PHP和C++工程师</span></a></li><li><a href='/adverts/41' target='_blank'><span style="color:red;font-weight:bold;">北京: 千橡集团暨校内网诚聘软件研发工程师</span></a></li><li><a href='/adverts/106' target='_blank'><span style="color:blue;font-weight:bold;">JavaEye问答大赛开始了！ 从6月23日 至 7月6日，奖品丰厚 ！</span></a></li><li><a href='/adverts/92' target='_blank'><span style="color:red;font-weight:bold;">快来参加7月17日在成都举行的SOA中国技术论坛</span></a></li></ul>
          <br/><br/><br/>
          ]]>
        </description>
        <pubDate>Mon, 03 Mar 2008 19:29:04 +0800</pubDate>
        <link>http://simohayha.javaeye.com/blog/167057</link>
        <guid>http://simohayha.javaeye.com/blog/167057</guid>
      </item>
      <item>
        <title>一个检测方法的参数类型的小程序</title>
        <author>simohayha</author>
        <description>
          <![CDATA[
          <br/>
          作者: <a href="http://simohayha.javaeye.com">simohayha</a>&nbsp;
          链接：<a href="http://simohayha.javaeye.com/blog/166705" style="color:red;">http://simohayha.javaeye.com/blog/166705</a>&nbsp;
          发表时间: 2008年03月02日
          <br/><br/>
          声明：本文系JavaEye网站发布的原创博客文章，未经作者书面许可，严禁任何网站转载本文，否则必将追究法律责任！
          <br/><br/>
          今天没事看blog的时候，看到一个小子实现了这个，不过他的程序写的那叫一个丑陋，实在是看不下去，就自己写了个.<br /><br />原理很简单，那就是首先将要检测的方法重命名掉，然后动态定义一个相同的，这时他就会调用我们定义的这个方法，这时我们就可以检测参数的类型的，检测如果成功，在最后调用我们刚才重命名过的那个方法:<br /><br /><pre name="code" class="ruby">
class Object
	class &lt;&lt; self
		def check_type method_name,*arg_type
			raise ArgumentError, "method_name is missing"       unless new.respond_to?(method_name)
			arg_count=new.method(method_name).arity
			alias_method :"old_#{method_name}",method_name
			define_method(method_name) do |*args|
				raise ArgumentError,"wrong number of arguments(#{args.length} of #{arg_count})" if arg_count>=0 and arg_count!=args.length
				message=""
				args.each_with_index do |item,index|
					break if arg_type[index]==nil
					message &lt;&lt; "argument type is wrong(#{item.class} of #{arg_type[index]}" if item.class!=arg_type[index]
				end
				raise ArgumentError,message unless message==""
				send(:"old_#{method_name}",*args)
			end
		end


	end
end
class Test
	def bar(x,y,z)
		p [x,y,z]
	end
       check_type :bar,String
end

Test.new.bar(1,2,5)
Test.new.bar("a",1,2)
</pre>
          <br/>
          <span style="color:red;">
            <a href="http://simohayha.javaeye.com/blog/166705#comments" style="color:red;">本文的讨论也很精彩，浏览讨论>></a>
          </span>
          <br/><br/><br/>
          <span style="color:#E28822;">JavaEye推荐</span>
          <br/>
          <ul class='adverts'><li><a href='/adverts/41' target='_blank'><span style="color:red;font-weight:bold;">北京: 千橡集团暨校内网诚聘软件研发工程师</span></a></li><li><a href='/adverts/92' target='_blank'><span style="color:red;font-weight:bold;">快来参加7月17日在成都举行的SOA中国技术论坛</span></a></li><li><a href='/adverts/106' target='_blank'><span style="color:blue;font-weight:bold;">JavaEye问答大赛开始了！ 从6月23日 至 7月6日，奖品丰厚 ！</span></a></li><li><a href='/adverts/42' target='_blank'><span style="color:red;font-weight:bold;">搜狐网站诚聘Java、PHP和C++工程师</span></a></li><li><a href='/adverts/97' target='_blank'><span style="color:blue;font-weight:bold;">Oracle专区上线，有Oracle最新文章，重要下载及知识库等精彩内容，欢迎访问。</span></a></li></ul>
          <br/><br/><br/>
          ]]>
        </description>
        <pubDate>Sun, 02 Mar 2008 22:48:06 +0800</pubDate>
        <link>http://simohayha.javaeye.com/blog/166705</link>
        <guid>http://simohayha.javaeye.com/blog/166705</guid>
      </item>
      <item>
        <title>[转帖]Joel Spolsky对于in-house programmer的定义</title>
        <author>simohayha</author>
        <description>
          <![CDATA[
          <br/>
          作者: <a href="http://simohayha.javaeye.com">simohayha</a>&nbsp;
          链接：<a href="http://simohayha.javaeye.com/blog/166555" style="color:red;">http://simohayha.javaeye.com/blog/166555</a>&nbsp;
          发表时间: 2008年03月01日
          <br/><br/>
          声明：本文系JavaEye网站发布的原创博客文章，未经作者书面许可，严禁任何网站转载本文，否则必将追究法律责任！
          <br/><br/>
          很早的文章了，转自这里：<br /><br />http://www.ruanyifeng.com/blog/2007/12/joel_spolsky_talk_at_yale_part_i.html<br /><br />这里也有节选:<br /><br />http://blog.csdn.net/pongba/archive/2007/12/05/1918649.aspx<br /><br />把两个的结合起来了，不过pongba老大的文笔更风趣，哈哈.<br /><br /><br />后来，Joel回忆起来，认为这是他一生中最痛苦的日子，并且劝告计算机系的学生尽可能不要去做“in-house programmer”。<br /><br />原因有三个：<br /><br /><div class="quote_title">引用</div><div class="quote_div"> 首先，你永远没有办法正确地编写软件，你不得不用最方便的方法编写软件。因为软件支出非常高昂，所以<span style="color: red"><span style="font-size: medium">公司会要求尽可能节省成本，你不可能试用新技术，只能使用现有的最成熟、最保守的技术。</span></span><br /><br />其次，<span style="color: red"><span style="font-size: medium">你没有办法将一个项目做到尽善尽美。一旦程序可以正常运行，你的工作也就结束了，可以接下去干公司的下一个项目了。你的作用是解决问题，而不是将软件写得尽可能好。</span></span>如果你是在一个专业的技术性公司，比如Google或Facebook，情况就完全不一样，你的软件写得越好，公司就会越成功，所以公司会支持你在一个项目上不断做下去。<br /><br />比如说，IBM，提供程序员给他们做软件，而IBM狮子大开口——300刀每小时——提供一个学了6星期"叨耐特"的毕业生，你说雇主还会让你慢慢磨蹭把功能完善更完善？不不，<span style="color: red"><span style="font-size: medium">只要主要功能实现，管什么界面，管什么鲁棒性，赶紧拍屁股走人，或者拍屁股去修补别人去年留下的烂摊子。于是，你搞出来的东西就跟狗啃的一样而且你还不能啥啥语言酷就用啥啥语言，Ruby？Ajax？不不，您老还是打开Visual Studio拖几个控件，连个把数据库，赶紧把功能给搞定了吧。300刀一小时，你拿不到，我还是要掏腰包的。所以，忘了"编程的艺术"这种crap吧，忘了作为程序员的荣耀和自豪吧... 那些都是虚名，就像浮云一样</span></span>... 27年以后他们会发给你你一个纪念金表...噢不，现在已经不给金表了，现在，27年以后你得到的是腕管综合症（控件拖多了吧小样？）。所以说啊，叫你千万别做in-house吧，做in-house软件是不好滴，如果做in-house软件好我不可能不让你做in-house软件啊，你想做in-house软件吗...？<br /><br />最后，传统公司IT部门里的程序员，只属于公司内部的维护人员，而不是直接从事核心业务的人员。因此，你永远办法进入管理层。但是，在技术性公司，程序员会变成CEO。<br /><br /><br />it’s frightening because this is what probably 80% of programming jobs are like, and if you’re not very, very careful when you graduate, you might find yourself working on in-house software, by accident, and let me tell you, it can drain the life out of you.</div><br /><br />自己现在就是在这80%的程序员里面，完完全全是一个in-house programmer。
          <br/>
          <span style="color:red;">
            <a href="http://simohayha.javaeye.com/blog/166555#comments" style="color:red;">本文的讨论也很精彩，浏览讨论>></a>
          </span>
          <br/><br/><br/>
          <span style="color:#E28822;">JavaEye推荐</span>
          <br/>
          <ul class='adverts'><li><a href='/adverts/42' target='_blank'><span style="color:red;font-weight:bold;">搜狐网站诚聘Java、PHP和C++工程师</span></a></li><li><a href='/adverts/92' target='_blank'><span style="color:red;font-weight:bold;">快来参加7月17日在成都举行的SOA中国技术论坛</span></a></li><li><a href='/adverts/41' target='_blank'><span style="color:red;font-weight:bold;">北京: 千橡集团暨校内网诚聘软件研发工程师</span></a></li><li><a href='/adverts/106' target='_blank'><span style="color:blue;font-weight:bold;">JavaEye问答大赛开始了！ 从6月23日 至 7月6日，奖品丰厚 ！</span></a></li><li><a href='/adverts/97' target='_blank'><span style="color:blue;font-weight:bold;">Oracle专区上线，有Oracle最新文章，重要下载及知识库等精彩内容，欢迎访问。</span></a></li></ul>
          <br/><br/><br/>
          ]]>
        </description>
        <pubDate>Sat, 01 Mar 2008 22:08:29 +0800</pubDate>
        <link>http://simohayha.javaeye.com/blog/166555</link>
        <guid>http://simohayha.javaeye.com/blog/166555</guid>
      </item>
      <item>
        <title>4年一次的日子</title>
        <author>simohayha</author>
        <description>
          <![CDATA[
          <br/>
          作者: <a href="http://simohayha.javaeye.com">simohayha</a>&nbsp;
          链接：<a href="http://simohayha.javaeye.com/blog/166296" style="color:red;">http://simohayha.javaeye.com/blog/166296</a>&nbsp;
          发表时间: 2008年02月29日
          <br/><br/>
          声明：本文系JavaEye网站发布的原创博客文章，未经作者书面许可，严禁任何网站转载本文，否则必将追究法律责任！
          <br/><br/>
          也不知道写些什么，贰李子他们说这是个4年一次的好日子，一定要写些什么，所以就随便乱写了。<br /><br /><br />只想告诉贰李子，胖马，贰王子，大s和小潘。我爱你们。。哈哈哈哈<br /><br />还有希望这次能够美梦成真，那就能和贰李子还有贰王子聚一聚了...<img src="/images/smiles/icon_biggrin.gif"/>
          <br/>
          <span style="color:red;">
            <a href="http://simohayha.javaeye.com/blog/166296#comments" style="color:red;">本文的讨论也很精彩，浏览讨论>></a>
          </span>
          <br/><br/><br/>
          <span style="color:#E28822;">JavaEye推荐</span>
          <br/>
          <ul class='adverts'><li><a href='/adverts/41' target='_blank'><span style="color:red;font-weight:bold;">北京: 千橡集团暨校内网诚聘软件研发工程师</span></a></li><li><a href='/adverts/42' target='_blank'><span style="color:red;font-weight:bold;">搜狐网站诚聘Java、PHP和C++工程师</span></a></li><li><a href='/adverts/97' target='_blank'><span style="color:blue;font-weight:bold;">Oracle专区上线，有Oracle最新文章，重要下载及知识库等精彩内容，欢迎访问。</span></a></li><li><a href='/adverts/92' target='_blank'><span style="color:red;font-weight:bold;">快来参加7月17日在成都举行的SOA中国技术论坛</span></a></li><li><a href='/adverts/106' target='_blank'><span style="color:blue;font-weight:bold;">JavaEye问答大赛开始了！ 从6月23日 至 7月6日，奖品丰厚 ！</span></a></li></ul>
          <br/><br/><br/>
          ]]>
        </description>
        <pubDate>Fri, 29 Feb 2008 23:02:37 +0800</pubDate>
        <link>http://simohayha.javaeye.com/blog/166296</link>
        <guid>http://simohayha.javaeye.com/blog/166296</guid>
      </item>
      <item>
        <title>rails中的BlankSlate源码分析</title>
        <author>simohayha</author>
        <description>
          <![CDATA[
          <br/>
          作者: <a href="http://simohayha.javaeye.com">simohayha</a>&nbsp;
          链接：<a href="http://simohayha.javaeye.com/blog/165978" style="color:red;">http://simohayha.javaeye.com/blog/165978</a>&nbsp;
          发表时间: 2008年02月28日
          <br/><br/>
          声明：本文系JavaEye网站发布的原创博客文章，未经作者书面许可，严禁任何网站转载本文，否则必将追究法律责任！
          <br/><br/>
          其实这个类实现的功能很简单，那就是实现一个没有predefined 方法的类(除了__send__与__id__).自己也尝试着写了一个，思路什么和BlankSlate很接近，不过代码就丑陋多了。<br /><br />当写这段代码之前，我们先要分析一下如何才能去掉predefined的方法，这里有三种predefined的方法。<br /><br />1 Object类本身自带的一些实例方法.<br /><br />2 打开Kernel模块，或者Object类，然后动态的加入的方法。<br /><pre name="code" class="ruby">module Kernel
  def name1
    "bo"
  end
end
class Object
  def name2
    "bo"
  end
end</pre><br /><br />3 Object类include的模块里面所含有的方法:<br /><br /><pre name="code" class="ruby">module Name
  def name3
    "bo"
  end
end
class Object
  include Name
end</pre><br /><br />接下来看代码，我在里面会有注释 :<br /><br /><pre name="code" class="ruby">class BlankSlate
  class &lt;&lt; self

#首先，我们定义一个hide方法，这个方法就是隐藏掉BlankSlate类中名字为name的方法，可是instance_eval方法，或者__开头的方法，并不需要被隐藏.
    def hide(name)
      if instance_methods.include?(name.to_s) and
        name !~ /^(__|instance_eval)/
        @hidden_methods ||= {}
        @hidden_methods[name.to_sym] = instance_method(name)
        undef_method name
      end
    end
#这里提供一个可以查找一个方法是否已被隐藏的方法
    def find_hidden_method(name)
      @hidden_methods ||= {}
      @hidden_methods[name] || superclass.find_hidden_method(name)
    end
#这个是为了恢复所被隐藏的方法,首先使用上面的方法查找到方法，(这里使用了define_method,这个方法其实也就是动态的定义一个方法，name就是他的名字，详细的用法可以看我前面的文章)
    def reveal(name)
      bound_method = nil
      unbound_method = find_hidden_method(name)
      fail "Don't know how to reveal method '#{name}'" unless unbound_method
      define_method(name) do |*args|
        bound_method ||= unbound_method.bind(self)
        bound_method.call(*args)
      end
    end
  end
#这里执行hide操作
  instance_methods.each { |m| hide(m) }
end

######################################################################

#这里解决了第二个问题，那就是当一个方法被加入的时候，能够hide这个方法(这里使用了method_added(这个方法我认为是当加入了一个方法之后，然后就会调用这个方法，name就是你所加入的方法的名字）.
module Kernel
  class &lt;&lt; self
    alias_method :blank_slate_method_added, :method_added
    def method_added(name)
      result = blank_slate_method_added(name)
      return result if self != Kernel
      BlankSlate.hide(name)
      result
    end
  end
end

这里其实我们也可以这么做:


module Kernel
  class &lt;&lt; self
    # Detect method additions to Kernel and remove them in the
    # BlankSlate class.
    def method_added(name)
      return super if self != Kernel
      BlankSlate.hide(name)
      super
    end
  end
end


######################################################################
#这个和上面一样的。
class Object
  class &lt;&lt; self
    alias_method :blank_slate_method_added, :method_added

    # Detect method additions to Object and remove them in the
    # BlankSlate class.
    def method_added(name)
      result = blank_slate_method_added(name)
      return result if self != Object
      BlankSlate.hide(name)
      result
    end

    def find_hidden_method(name)
      nil
    end
  end
end

######################################################################

#这里是为了解决第三个问题，要注意的是append_features就是include时发生的动作，也就可以说，他其实就是include.所以这里我们必须先保存老的append_features方法，先加入mod方法，然后undef它，最后再返回其他的方法.
class Module
  alias blankslate_original_append_features append_features
  def append_features(mod)
    result = blankslate_original_append_features(mod)
    return result if mod != Object
    instance_methods.each do |name|
      BlankSlate.hide(name)
    end
    result
  end
end</pre>
          <br/>
          <span style="color:red;">
            <a href="http://simohayha.javaeye.com/blog/165978#comments" style="color:red;">本文的讨论也很精彩，浏览讨论>></a>
          </span>
          <br/><br/><br/>
          <span style="color:#E28822;">JavaEye推荐</span>
          <br/>
          <ul class='adverts'><li><a href='/adverts/106' target='_blank'><span style="color:blue;font-weight:bold;">JavaEye问答大赛开始了！ 从6月23日 至 7月6日，奖品丰厚 ！</span></a></li><li><a href='/adverts/42' target='_blank'><span style="color:red;font-weight:bold;">搜狐网站诚聘Java、PHP和C++工程师</span></a></li><li><a href='/adverts/97' target='_blank'><span style="color:blue;font-weight:bold;">Oracle专区上线，有Oracle最新文章，重要下载及知识库等精彩内容，欢迎访问。</span></a></li><li><a href='/adverts/92' target='_blank'><span style="color:red;font-weight:bold;">快来参加7月17日在成都举行的SOA中国技术论坛</span></a></li><li><a href='/adverts/41' target='_blank'><span style="color:red;font-weight:bold;">北京: 千橡集团暨校内网诚聘软件研发工程师</span></a></li></ul>
          <br/><br/><br/>
          ]]>
        </description>
        <pubDate>Thu, 28 Feb 2008 23:27:59 +0800</pubDate>
        <link>http://simohayha.javaeye.com/blog/165978</link>
        <guid>http://simohayha.javaeye.com/blog/165978</guid>
      </item>
      <item>
        <title>javascript中的继承</title>
        <author>simohayha</author>
        <description>
          <![CDATA[
          <br/>
          作者: <a href="http://simohayha.javaeye.com">simohayha</a>&nbsp;
          链接：<a href="http://simohayha.javaeye.com/blog/165606" style="color:red;">http://simohayha.javaeye.com/blog/165606</a>&nbsp;
          发表时间: 2008年02月27日
          <br/><br/>
          声明：本文系JavaEye网站发布的原创博客文章，未经作者书面许可，严禁任何网站转载本文，否则必将追究法律责任！
          <br/><br/>
          首先在js中的继承是Prototype-based 。不像一般的oo语言，比如java,c++是Class-based 的。<br /><br />我们来比较一下这两种方式 :<br /><br />1 在Class-based 中的类和实例是不同的实体，而在Prototype-based中所有对象都是实例。<br /><br />2 在Class-based中定义一个类使用class关键字来定义。实例化一个对象，使用构造方法。而在Prototype-based，定义和创建一个对象都使用构造器函数。<br /><br />3 在Class-based中构造一个继承，需要定义一个类，然后作为存在的类的子类。而在Prototype-based中，则是需要标记这个对象作为构造器函数的prototype 。<br /><br />4 在Class-based中继承属性通过class chain。而在Prototype-based则是通过prototype chain。<br /><br />下来我们来看一个我们需要实现的继承体系(后面有图)：<br /><br />Manager 和WorkerBee 继承Employee ，而SalesPerson 和Engineer继承WorkerBee.<br /><br />其中，Employee有一个name 属性，Manager有一个reports 属性，SalesPerson 有一个quota 属性，Engineer有一个machine 属性。<br /><br /><pre name="code" class="javascript">function Employee () {
this.name = "";
this.dept = "general";
} 

function Manager () {
this.reports = [];
}
Manager.prototype = new Employee; 
function WorkerBee () {
this.projects = [];
}
WorkerBee.prototype = new Employee; 
function SalesPerson () {
   this.dept = "sales";
   this.quota = 100;
}
SalesPerson.prototype = new WorkerBee; 
function Engineer () {
   this.dept = "engineering";
   this.machine = "";
}
Engineer.prototype = new WorkerBee; </pre><br /><br />这只是一个非常非常简单的例子，不过这里我们可以看到在javascript中的继承，就是通过构造函数的prototype来进行构造的。<br /><br /><br /><br />下面又有一个问题，那就是想给一个对象在运行时加入属性怎么办，或许你想到该这么做：<br /><br /><pre name="code" class="javascript">mark.bonus = 3000</pre><br /><br />可是这样做的话只是mark 对象拥有了一个bonus属性，而不是WorkerBee 拥有这个属性。<br /><br />如果你想要加一个属性被这个构造函数的所有对象所共享，那么你就必须这么做：<br /><br /><pre name="code" class="java">Employee.prototype.specialty = "none"; </pre><br /><br />这里的话，就是给整个继承体系加入了一个specialty属性。<br /><br />现在假设我们有下面的代码(这里已经给各个构造器加入了一些参数：<br /><br /><pre name="code" class="java">jane = new Engineer("belau"); </pre><br /><br />然后jane的属性的值如下：<br /><br /><div class="quote_title">引用</div><div class="quote_div">jane.name == "";<br />jane.dept == "general";<br />jane.projects == [];<br />jane.machine == "belau</div><br /><br />可以发现这时我们无法指定一个继承属性的值，不过没关系，我们可以这样修改我们的程序：<br /><br /><pre name="code" class="java">function Employee (name, dept) {
this.name = name || "";
this.dept = dept || "general";
}

function Engineer (name, projs, mach) {
this.base = WorkerBee;
this.base(name, "engineering", projs);
this.machine = mach || "";
} 

function WorkerBee (name,dept,projs) {
this.base=Employee;
this.base(name,dept);
this.projects = projs || [];
}
WorkerBee.prototype = new Employee; </pre><br /><br /><br />现在我们可以分析下<br />jane = new Engineer("Doe, Jane", ["navigator", "javascript"], "belau"); <br />调用这句时，都发生了什么:<br /><br />1  当js看见new操作符，它创建一个新的普通对象,并且设置它的__proto__ 属性为Engineer.prototype。<br /><br />2 new 操作符传递这个新的对象作为Engineer 构造器的this的值。<br /><br />其实最主要做的事就是上面的两件，剩下的都是很简单的函数调用.<br /><br /><br /><br />我们来看一下在继承中，假设你要存取一个对象的一个属性时会发生什么：<br /><br />1 首先检测这个值是否在本地存在。<br /><br />2 如果不存在本地变量，那么就检测prototype chain （使用__proto__ ）。<br /><br />3 如果属性在prototype chain中存在那么就返回这个值，否则这个对象就不存在这个值。<br /><br />比如下面的例子：<br /><br /><pre name="code" class="java">function Employee () {
   this.dept = "general";
}
Employee.prototype.name = ""; 

function WorkerBee () {
this.projects = [];
}
WorkerBee.prototype = new Employee; 

amy = new WorkerBee; 

Employee.prototype.name = "Unknown"; </pre><br /><br />可以看到打印出Unknown.<br /><br />通过上面我们知道当你使用new操作符创建一个对象的时候，js会设置这个对象的__proto__作为构造器函数的prototype ，因此我们能够通过这个来测试prototype chain。<br /><br />假设我们创建了一个chris 对象：<br /><pre name="code" class="java">
chris = new Engineer("Pigman, Chris", ["jsd"], "fiji");</pre><br /><br />那么下面的语句都会返回true:<br /><br /><pre name="code" class="javascript">chris.__proto__ == Engineer.prototype;
chris.__proto__.__proto__ == WorkerBee.prototype;
chris.__proto__.__proto__.__proto__ == Employee.prototype;
chris.__proto__.__proto__.__proto__.__proto__ == Object.prototype;
chris.__proto__.__proto__.__proto__.__proto__.__proto__ == null;</pre><br /><br />最后要说的是，在js中并没有提供多重继承.<br /><br />不过我们可以这样模拟实现：<br /><br /><br /><pre name="code" class="javascript">function Hobbyist (hobby) {
   this.hobby = hobby || "scuba";
} 

function Engineer (name, projs, mach, hobby) {
   this.base1 = WorkerBee;
   this.base1(name, "engineering", projs);
   this.base2 = Hobbyist;
   this.base2(hobby);
   this.machine = mach || "";
}
Engineer.prototype = new WorkerBee; 

dennis = new Engineer("Doe, Dennis", ["collabra"], "hugo") </pre><br /><br />这样做不是很好，这是因为每次new的时候都会分配Hobbyist的空间。<br /><br />可是如果我们给Hobbyist添加一个属性的话，dennis 对象将不能继承这个新的属性.
          <br/>
          <span style="color:red;">
            <a href="http://simohayha.javaeye.com/blog/165606#comments" style="color:red;">本文的讨论也很精彩，浏览讨论>></a>
          </span>
          <br/><br/><br/>
          <span style="color:#E28822;">JavaEye推荐</span>
          <br/>
          <ul class='adverts'><li><a href='/adverts/92' target='_blank'><span style="color:red;font-weight:bold;">快来参加7月17日在成都举行的SOA中国技术论坛</span></a></li><li><a href='/adverts/106' target='_blank'><span style="color:blue;font-weight:bold;">JavaEye问答大赛开始了！ 从6月23日 至 7月6日，奖品丰厚 ！</span></a></li><li><a href='/adverts/42' target='_blank'><span style="color:red;font-weight:bold;">搜狐网站诚聘Java、PHP和C++工程师</span></a></li><li><a href='/adverts/41' target='_blank'><span style="color:red;font-weight:bold;">北京: 千橡集团暨校内网诚聘软件研发工程师</span></a></li><li><a href='/adverts/97' target='_blank'><span style="color:blue;font-weight:bold;">Oracle专区上线，有Oracle最新文章，重要下载及知识库等精彩内容，欢迎访问。</span></a></li></ul>
          <br/><br/><br/>
          ]]>
        </description>
        <pubDate>Wed, 27 Feb 2008 23:44:02 +0800</pubDate>
        <link>http://simohayha.javaeye.com/blog/165606</link>
        <guid>http://simohayha.javaeye.com/blog/165606</guid>
      </item>
      <item>
        <title>在javascript中如何判断一个方法是new调用还是一般调用</title>
        <author>simohayha</author>
        <description>
          <![CDATA[
          <br/>
          作者: <a href="http://simohayha.javaeye.com">simohayha</a>&nbsp;
          链接：<a href="http://simohayha.javaeye.com/blog/165475" style="color:red;">http://simohayha.javaeye.com/blog/165475</a>&nbsp;
          发表时间: 2008年02月27日
          <br/><br/>
          声明：本文系JavaEye网站发布的原创博客文章，未经作者书面许可，严禁任何网站转载本文，否则必将追究法律责任！
          <br/><br/>
          在看The.Art.and.Science.of.JavaScript 的时候，看到了这个问题，就是在javascript中如何判断一个方法是new调用还是一般调用，在书中给出的代码是<br /><br /><pre name="code" class="javascript">function Element(){
   if(this==window || 'Element' in this){
      console.log("normal");
}else{
     console.log("new");
}

}</pre><br /><br />可是这个代码不能解决两个问题:<br /><br />1比如加上下面的代码：<br /><br /><pre name="code" class="javascript"> Element.prototype.Element=1; </pre><br /><br />这时如果调用new Element()时，由于Element 已经存在于this里面，因此会打印出normal;<br /><br />这个很好解决。我是这样写的:<br /><br /><pre name="code" class="javascript">function Element(){
if(this.__proto__==Element.prototype)
{console.log("new");}else{console.log("normal");}}</pre><br /><br />可是这时又有了第二个问题。<br /><br />比如下面的代码:<br /><br /><pre name="code" class="javascript">var test= new Element();

test.temp=Element;

test.temp();
</pre><br /><br />这时的调用，也应该是normal的调用，可是依然打印出new.<br /><br />实在想不出来怎么解决第二种情况，因此发出来看那位能够解决这个问题.
          <br/>
          <span style="color:red;">
            <a href="http://simohayha.javaeye.com/blog/165475#comments" style="color:red;">本文的讨论也很精彩，浏览讨论>></a>
          </span>
          <br/><br/><br/>
          <span style="color:#E28822;">JavaEye推荐</span>
          <br/>
          <ul class='adverts'><li><a href='/adverts/42' target='_blank'><span style="color:red;font-weight:bold;">搜狐网站诚聘Java、PHP和C++工程师</span></a></li><li><a href='/adverts/97' target='_blank'><span style="color:blue;font-weight:bold;">Oracle专区上线，有Oracle最新文章，重要下载及知识库等精彩内容，欢迎访问。</span></a></li><li><a href='/adverts/92' target='_blank'><span style="color:red;font-weight:bold;">快来参加7月17日在成都举行的SOA中国技术论坛</span></a></li><li><a href='/adverts/106' target='_blank'><span style="color:blue;font-weight:bold;">JavaEye问答大赛开始了！ 从6月23日 至 7月6日，奖品丰厚 ！</span></a></li><li><a href='/adverts/41' target='_blank'><span style="color:red;font-weight:bold;">北京: 千橡集团暨校内网诚聘软件研发工程师</span></a></li></ul>
          <br/><br/><br/>
          ]]>
        </description>
        <pubDate>Wed, 27 Feb 2008 15:28:36 +0800</pubDate>
        <link>http://simohayha.javaeye.com/blog/165475</link>
        <guid>http://simohayha.javaeye.com/blog/165475</guid>
      </item>
      <item>
        <title>ruby中的类变量与类实例变量</title>
        <author>simohayha</author>
        <description>
          <![CDATA[
          <br/>
          作者: <a href="http://simohayha.javaeye.com">simohayha</a>&nbsp;
          链接：<a href="http://simohayha.javaeye.com/blog/165269" style="color:red;">http://simohayha.javaeye.com/blog/165269</a>&nbsp;
          发表时间: 2008年02月26日
          <br/><br/>
          声明：本文系JavaEye网站发布的原创博客文章，未经作者书面许可，严禁任何网站转载本文，否则必将追究法律责任！
          <br/><br/>
          首先，在ruby1.8中类变量是所有子类和父类共享的，可以看下面的代码：<br /><br /><pre name="code" class="ruby">class IntelligentLife  
  @@home_planet = nil
  
  def self.home_planet
    @@home_planet
  end
  def self.home_planet=(x)
    @@home_planet = x
  end
  #...
end
class Terran &lt; IntelligentLife
  @@home_planet = "Earth"
end

class Martian &lt; IntelligentLife
  @@home_planet = "Mars"
end


p IntelligentLife.home_planet
p Terran.home_planet
p Martian.home_planet</pre><br /><br />可以看到结果是相同的，都是"Mars".这是因为父类的类变量是被整个继承体系所共享的.<br /><br />在这里我们如果想要得到我们所需要的结果，我们就要使用类实例变量，因为类实例变量是严格的per-class。而不是被整个继承体系所共享。<br /><br /><pre name="code" class="java">class IntelligentLife  
  @home_planet = nil
  
 class &lt;&lt; self
    attr_accessor :home_planet
  end
end
class Terran &lt; IntelligentLife
  @home_planet = "Earth"
end

class Martian &lt; IntelligentLife
  @home_planet = "Mars"
end


p IntelligentLife.home_planet
p Terran.home_planet
p Martian.home_planet</pre><br /><br /><br />而在1.9中，类变量不再被继承体系所共享。也就是和java中的静态变量差不多了。<br /><br />下面的代码在1.9中运行.<br /><br /><pre name="code" class="ruby">class IntelligentLife  
  @@home_planet = "bobo"
  def self.home_planet
    @@home_planet
  end
end
class Terran &lt; IntelligentLife
  @@home_planet = "Earth" 
end

class Martian &lt; IntelligentLife
  @@home_planet = "Mars"
  def self.home_planet
    @@home_planet
  end
end


p IntelligentLife.home_planet
p Terran.home_planet
p Martian.home_planet</pre><br /><br />可以看到这时的类变量才是真正的类变量，也就是说是per-class的了.
          <br/>
          <span style="color:red;">
            <a href="http://simohayha.javaeye.com/blog/165269#comments" style="color:red;">本文的讨论也很精彩，浏览讨论>></a>
          </span>
          <br/><br/><br/>
          <span style="color:#E28822;">JavaEye推荐</span>
          <br/>
          <ul class='adverts'><li><a href='/adverts/97' target='_blank'><span style="color:blue;font-weight:bold;">Oracle专区上线，有Oracle最新文章，重要下载及知识库等精彩内容，欢迎访问。</span></a></li><li><a href='/adverts/106' target='_blank'><span style="color:blue;font-weight:bold;">JavaEye问答大赛开始了！ 从6月23日 至 7月6日，奖品丰厚 ！</span></a></li><li><a href='/adverts/42' target='_blank'><span style="color:red;font-weight:bold;">搜狐网站诚聘Java、PHP和C++工程师</span></a></li><li><a href='/adverts/92' target='_blank'><span style="color:red;font-weight:bold;">快来参加7月17日在成都举行的SOA中国技术论坛</span></a></li><li><a href='/adverts/41' target='_blank'><span style="color:red;font-weight:bold;">北京: 千橡集团暨校内网诚聘软件研发工程师</span></a></li></ul>
          <br/><br/><br/>
          ]]>
        </description>
        <pubDate>Tue, 26 Feb 2008 21:15:50 +0800</pubDate>
        <link>http://simohayha.javaeye.com/blog/165269</link>
        <guid>http://simohayha.javaeye.com/blog/165269</guid>
      </item>
      <item>
        <title>在ubuntu上共存多个版本的ruby</title>
        <author>simohayha</author>
        <description>
          <![CDATA[
          <br/>
          作者: <a href="http://simohayha.javaeye.com">simohayha</a>&nbsp;
          链接：<a href="http://simohayha.javaeye.com/blog/164574" style="color:red;">http://simohayha.javaeye.com/blog/164574</a>&nbsp;
          发表时间: 2008年02月24日
          <br/><br/>
          声明：本文系JavaEye网站发布的原创博客文章，未经作者书面许可，严禁任何网站转载本文，否则必将追究法律责任！
          <br/><br/>
          今天装Revactor库的时候，真把我郁闷了，没想到ubuntu源里面的ruby1.9装了之后，根本不能用gem来安装Revactor.最后google到了一篇文章，原来是要自己编译ruby1.9，然后才能正常使用gem.<br />源里面安装的ruby1.9，什么都没有，gem,rake什么的都没有，所以我们要自己编译,安装.<br /><br />原文在这里：<br />http://blog.michaelgreenly.com/2007/12/multiple-ruby-version-on-ubuntu.html<br /><br /><br /><br />假设你已经通过apt-get安装了ruby1.8,下来我们要安装ruby1.9<br /><pre name="code" class="java">
sudo apt-get build-dep ruby1.9
wget ftp://ftp.ruby-lang.org/pub/ruby/1.9/ruby-1.9.0-0.tar.gz
tar -xvzf ruby-1.9.0-0.tar.gz
cd ruby-1.9*
./configure --prefix=/opt/ruby1.9 --program-suffix=1.9.0
sudo make && make install
sudo ln -s /opt/ruby1.9.0/bin/* /usr/local/bin</pre><br /><br />这下我们就能用gem1.9.0 install Revactor来安装了。<br /><br />不过我运行gem1.9.0 update --system的时候，直接报错：<br /><br /><pre name="code" class="java">setup.rb:12:in `&lt;main>': uninitialized constant Config (NameError)</pre><br /><br />不知道什么原因..
          <br/>
          <span style="color:red;">
            <a href="http://simohayha.javaeye.com/blog/164574#comments" style="color:red;">本文的讨论也很精彩，浏览讨论>></a>
          </span>
          <br/><br/><br/>
          <span style="color:#E28822;">JavaEye推荐</span>
          <br/>
          <ul class='adverts'><li><a href='/adverts/92' target='_blank'><span style="color:red;font-weight:bold;">快来参加7月17日在成都举行的SOA中国技术论坛</span></a></li><li><a href='/adverts/106' target='_blank'><span style="color:blue;font-weight:bold;">JavaEye问答大赛开始了！ 从6月23日 至 7月6日，奖品丰厚 ！</span></a></li><li><a href='/adverts/42' target='_blank'><span style="color:red;font-weight:bold;">搜狐网站诚聘Java、PHP和C++工程师</span></a></li><li><a href='/adverts/97' target='_blank'><span style="color:blue;font-weight:bold;">Oracle专区上线，有Oracle最新文章，重要下载及知识库等精彩内容，欢迎访问。</span></a></li><li><a href='/adverts/41' target='_blank'><span style="color:red;font-weight:bold;">北京: 千橡集团暨校内网诚聘软件研发工程师</span></a></li></ul>
          <br/><br/><br/>
          ]]>
        </description>
        <pubDate>Sun, 24 Feb 2008 15:20:11 +0800</pubDate>
        <link>http://simohayha.javaeye.com/blog/164574</link>
        <guid>http://simohayha.javaeye.com/blog/164574</guid>
      </item>
  </channel>
</rss>