亚洲av成人无遮挡网站在线观看,少妇性bbb搡bbb爽爽爽,亚洲av日韩精品久久久久久,兔费看少妇性l交大片免费,无码少妇一区二区三区

  免費注冊 查看新帖 |

Chinaunix

  平臺 論壇 博客 文庫
最近訪問板塊 發(fā)新帖
查看: 2395 | 回復: 1
打印 上一主題 下一主題

Eloquent Ruby 讀書筆記 續(xù) [復制鏈接]

論壇徽章:
0
跳轉到指定樓層
1 [收藏(0)] [報告]
發(fā)表于 2012-03-01 19:30 |只看該作者 |倒序瀏覽
Eloquent Ruby 讀書筆記 續(xù)





initializeruby
運算符重載和Ruby風格的運算符重載
程序大概意思是,重新定義,文件類的比較方法。只要目錄和文件名一樣,就說這兩個文件類一樣。ruby的
Ruby代碼
  1. class DocumentPointer   
  2.   attr_reader :folder, :name  
  3.   def initialize( folder, name )   
  4.     @folder = folder   
  5.     @name = name   
  6.   end  
  7.   def ==(other)   
  8.     return true if other.equal?(self)   
  9.     return false unless other.instance_of?(self.class)   
  10.     folder == other.folder && name == other.name   
  11.   end  
  12. end  

  13. class DocumentPointer
  14.   attr_reader :folder, :name
  15.   def initialize( folder, name )
  16.     @folder = folder
  17.     @name = name
  18.   end
  19.   def ==(other)
  20.     return true if other.equal?(self)
  21.     return false unless other.instance_of?(self.class)
  22.     folder == other.folder && name == other.name
  23.   end
  24. end
復制代碼
Ruby代碼
  1. class DocumentPointer   
  2.   attr_reader :folder, :name  
  3.   def initialize( folder, name )   
  4.     @folder = folder   
  5.     @name = name   
  6.   end  
  7.   def ==(other)   
  8.     return false unless other.respond_to?(:folder)   
  9.     return false unless other.respond_to?(:name)   
  10.     folder == other.folder && name == other.name   
  11.   end  
  12. end  

  13. class DocumentPointer
  14.   attr_reader :folder, :name
  15.   def initialize( folder, name )
  16.     @folder = folder
  17.     @name = name
  18.   end
  19.   def ==(other)
  20.     return false unless other.respond_to?(:folder)
  21.     return false unless other.respond_to?(:name)
  22.     folder == other.folder && name == other.name
  23.   end
  24. end
復制代碼
當我們用的時候
Ruby代碼
  1. #Asymmetry   
  2. class ContractIdentifier < DocumentIdentifier   
  3. end  
  4.   
  5. doc_id = DocumentIdentifier.new( 'contracts', 'Book Deal' )   
  6. con_id = ContractIdentifier.new( 'contracts', 'Book Deal' )   
  7.   
  8. puts "They are equal!" if doc_id == con_id  

  9. #Asymmetry
  10. class ContractIdentifier < DocumentIdentifier
  11. end

  12. doc_id = DocumentIdentifier.new( 'contracts', 'Book Deal' )
  13. con_id = ContractIdentifier.new( 'contracts', 'Book Deal' )

  14. puts "They are equal!" if doc_id == con_id
復制代碼
會出現子類沒有父類方法的問題,在contractIdentifier里沒有重載操作符。那么

Ruby代碼
  1. class VersionedIdentifier < DocumentIdentifier   
  2.   attr_reader :version  
  3.   
  4.   def initialize(folder, name, version)   
  5.     super(folder, name)   
  6.     Well-Behaved Equality 147   
  7.     @version = version   
  8.   end  
  9.   
  10.   def ==(other)   
  11.     if other.instance_of? VersionedIdentifier   
  12.       other.folder == folder &&   
  13.       other.name == name &&   
  14.       other.version == version   
  15.     elsif other.instance_of? DocumentIdentifier   
  16.       other.folder == folder && other.name == name   
  17.     else  
  18.       false  
  19.     end  
  20.   end  
  21. end  
復制代碼

論壇徽章:
0
2 [報告]
發(fā)表于 2012-03-01 22:36 |只看該作者
謝謝分享
您需要登錄后才可以回帖 登錄 | 注冊

本版積分規(guī)則 發(fā)表回復

  

北京盛拓優(yōu)訊信息技術有限公司. 版權所有 京ICP備16024965號-6 北京市公安局海淀分局網監(jiān)中心備案編號:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年舉報專區(qū)
中國互聯(lián)網協(xié)會會員  聯(lián)系我們:huangweiwei@itpub.net
感謝所有關心和支持過ChinaUnix的朋友們 轉載本站內容請注明原作者名及出處

清除 Cookies - ChinaUnix - Archiver - WAP - TOP