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

  免費(fèi)注冊(cè) 查看新帖 |

Chinaunix

  平臺(tái) 論壇 博客 文庫(kù)
最近訪問(wèn)板塊 發(fā)新帖
查看: 4763 | 回復(fù): 2
打印 上一主題 下一主題

python __cmp__ 重載 [復(fù)制鏈接]

論壇徽章:
0
跳轉(zhuǎn)到指定樓層
1 [收藏(0)] [報(bào)告]
發(fā)表于 2013-06-01 11:19 |只看該作者 |倒序?yàn)g覽
本帖最后由 ideal_gjh 于 2013-06-01 11:20 編輯
  1. >>>  class  Dog:
  2. ...         def  __cmp__(self,other):
  3. ... if  other  >  0  :  return  1;
  4. ... elif  other  <  0  :  return  -1;
  5. ... else:  return  0
  6. ...
  7. >>>  dog  =  Dog()
  8. >>>  if  dog  >  -10  :  print  "ok"
  9. ...
  10. >>>  if  dog  <  -10  :  print  "ok"
  11. ...
  12. ok
  13. >>>  if  dog  ==  0  :  print  "ok"
  14. ...
  15. ok
  16. >>>
復(fù)制代碼
這個(gè)代碼一直沒(méi)有理解,有人詳細(xì)解釋下嗎,3q.

論壇徽章:
0
2 [報(bào)告]
發(fā)表于 2013-06-01 13:35 |只看該作者
拿dog和數(shù)字比較的意義何在?

論壇徽章:
0
3 [報(bào)告]
發(fā)表于 2013-06-01 14:42 |只看該作者
回復(fù) 1# ideal_gjh


    這個(gè)例子是哪里來(lái)的啊,給你看個(gè)例子:http://www.tutorialspoint.com/python/python_classes_objects.htm
  1. Overloading Operators:
  2. Suppose you've created a Vector class to represent two-dimensional vectors. What happens when you use the plus operator to add them? Most likely Python will yell at you.

  3. You could, however, define the __add__ method in your class to perform vector addition, and then the plus operator would behave as per expectation:

  4. EXAMPLE:
  5. #!/usr/bin/python

  6. class Vector:
  7.    def __init__(self, a, b):
  8.       self.a = a
  9.       self.b = b

  10.    def __str__(self):
  11.       return 'Vector (%d, %d)' % (self.a, self.b)
  12.    
  13.    def __add__(self,other):
  14.       return Vector(self.a + other.a, self.b + other.b)

  15. v1 = Vector(2,10)
  16. v2 = Vector(5,-2)
  17. print v1 + v2
  18. When the above code is executed, it produces following result:

  19. Vector(7,8)
復(fù)制代碼
您需要登錄后才可以回帖 登錄 | 注冊(cè)

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

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP