乡下人产国偷v产偷v自拍,国产午夜片在线观看,婷婷成人亚洲综合国产麻豆,久久综合给合久久狠狠狠9

  • <output id="e9wm2"></output>
    <s id="e9wm2"><nobr id="e9wm2"><ins id="e9wm2"></ins></nobr></s>

    • 分享

      動(dòng)態(tài)語(yǔ)言ruby、groovy、python基本語(yǔ)法復(fù)習(xí)1

       weicat 2011-08-03

      動(dòng)態(tài)語(yǔ)言的豐盛大餐啊,不容錯(cuò)過(guò),下面來(lái)簡(jiǎn)單的復(fù)習(xí)一下這三門(mén)語(yǔ)言。。。

      ruby

      Ruby代碼
       收藏代碼
      1. # To change this template, choose Tools | Templates  
      2. # and open the template in the editor.  
      3.   
      4. puts "Hello World"  
      5. print 6/2  
      6. print 'hello'  
      7. puts 'hello'\  
      8.   'world'  
      9.   
      10. a=1  
      11. b=1.0  
      12. c=1.0  
      13. d=1.0  
      14. e=c  
      15. puts(a==b)#值相等  
      16. puts(a.eql?(b)) #值相等,類(lèi)型相等  
      17. puts(c.equal?(d))#值相等,內(nèi)存地址相等  
      18. puts(c.equal?(e))  
      19.   
      20. puts("abd" <=> "acd"#-1  
      21. puts((0..5) === 10) #false  
      22. puts((0..5) === 3.2) #true  
      23.   
      24.   
      25. x=3  
      26. case x  
      27. when 1..2  
      28.   print "x=",x,",在1..2中"  
      29. when 4..9,0  
      30.   print "x=",x,",在4..9,0中"  
      31. else  
      32.   print "x=",x,",其它可能"  
      33. end  
      34. #x=3,其它可能  
      35.   
      36. a=1  
      37. while( a < 10 )  
      38.   print(a," ")  
      39.   a=a+1  
      40. end  
      41.   
      42. b=1  
      43. until( b >= 10 )  
      44.   print(b," ")  
      45.   b=b+1  
      46. end  
      47. #1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9  
      48.   
      49. 3.times{print "hi"}  
      50. 1.upto(9){|i| print i if i<7}  
      51. 9.downto(1){|i| print i if i<7}  
      52. (1..9).each{|i| print i if i<7}  
      53. 0.step(11,3){|i| print i}  
      54. #hihihi1234566543211234560369  
      55.   
      56. a=5  
      57. b="hhhh"  
      58. print("a is ",a,"\n")    #a is 5  
      59. puts("a is #{a}")    #a is 5  
      60. puts('a is #{a}') #a is #{a}  
      61.   
      62. #ruby支持缺省參數(shù)  
      63. def sum(a,b=5)  
      64.   a+b  
      65. end  
      66. puts sum(3,6) #輸出結(jié)果為:9  
      67. puts sum(3)#輸出結(jié)果為8  
      68.   
      69. #ruby支持可變參數(shù)  
      70. def sum(*num)  
      71.   numSum = 0  
      72.   num.each{|i| numSum += i}  
      73.   return numSum  
      74. end  
      75.   
      76. puts sum() #輸出結(jié)果為0  
      77. puts sum(3,6)#輸出結(jié)果為9  
      78. puts sum(1,2,3,4,5,6,7,8,9)#輸出結(jié)果為45  
      79.   
      80. #ruby中如果一個(gè)類(lèi)里有2個(gè)同名方法,總是后面的一個(gè)被執(zhí)行  
      81.   
      82. #實(shí)例變量:每個(gè)實(shí)例獨(dú)享,變量名用@開(kāi)頭  
      83. #類(lèi)變量:所有實(shí)例共享,變量名用@@開(kāi)頭,類(lèi)似java里的static變量,但是在使用前必須要初始化。  
      84. #定義類(lèi)方法 如果在外部調(diào)用一個(gè)類(lèi)里的常量,需要用到域作用符號(hào)"::"  
      85. class StudentClass  
      86.   
      87. end  
      88. def StudentClass.student_count  
      89.   puts "aaa"  
      90. end  
      91.   
      92. #ruby里的單例方法:給具體的某個(gè)實(shí)例對(duì)象添加方法,這個(gè)方法只屬于這個(gè)實(shí)例對(duì)象的。這樣的方法叫單例方法  
      93. #定義單例方法,首先要生成一個(gè)實(shí)例對(duì)象,其次要在方法名前加上一個(gè)對(duì)象名和一個(gè)點(diǎn)號(hào)(.)  
      94. class Person  
      95.   def talk  
      96.     puts "hi!"  
      97.   end  
      98. end  
      99.   
      100. p1 = Person.new  
      101. p2 = Person.new  
      102.   
      103. def p2.talk #定義單例方法p2.talk  
      104.   puts "Here is p2."  
      105. end  
      106. def p2.laugh  
      107.   puts "ha,ha,ha..."  
      108. end  
      109.   
      110. p1.talk  
      111. p2.talk  
      112. p2.laugh  
      113.   
      114. #hi!  
      115. #Here is p2.  
      116. #ha,ha,ha...  
      117.   
      118. #訪問(wèn)控制  
      119. #public , protected, private  
      120. #public 方法,可以被定義它的類(lèi)和其子類(lèi)訪問(wèn),可以被類(lèi)和其子類(lèi)的實(shí)例對(duì)象調(diào)用  
      121. #protected 方法,可以被定義它的類(lèi)和其子類(lèi)訪問(wèn),不能被類(lèi)和其子類(lèi)的實(shí)例對(duì)象調(diào)用,但是 可以在類(lèi)和其子類(lèi)中制定給實(shí)例對(duì)象  
      122. #private 方法,可以被定義它的類(lèi)和其子類(lèi)訪問(wèn),不能被類(lèi)和其子類(lèi)的實(shí)例對(duì)象調(diào)用,私有方法不能指定對(duì)象  
      123.   
      124. class Person  
      125.   public  
      126.   def talk  
      127.     puts "public:talk"  
      128.   end  
      129.   def speak  
      130.     "protected:speak"  
      131.   end  
      132.   def laugh  
      133.     "private:laugh"  
      134.   end  
      135.   protected :speak  
      136.   private :laugh  
      137.   
      138.   def useLaughTest(another)  
      139.     puts another.laugh #這里錯(cuò)誤,私有方法不能指定對(duì)象  
      140.   end  
      141.   
      142.   def useSpeakTest(another)  
      143.     puts another.speak  #這里可以,,protected方法可以指定對(duì)象  
      144.   end  
      145. end  
      146.   
      147. class Student < Person  
      148.   def useLaugh  
      149.     puts laugh  
      150.   end  
      151.   def useSpeak  
      152.     puts speak  
      153.   end  
      154. end  
      155. puts '----------1'  
      156. p1 = Person.new  
      157. p1.talk  
      158. #p1.speak #實(shí)例對(duì)象不能訪問(wèn)protected方法  
      159. #p1.laugh #實(shí)例對(duì)象不能訪問(wèn)private方法  
      160. puts '----------2'  
      161. p2 = Student.new  
      162. p2.useLaugh  
      163. puts '----------3'  
      164. p2.useSpeak  

       

      groovy

      Groovy代碼
       收藏代碼
      1. /*  
      2.  * To change this template, choose Tools | Templates  
      3.  * and open the template in the editor.  
      4.  */  
      5.   
      6. package javaapplication1  
      7.   
      8. /**  
      9.  *  
      10.  * @author zsbz  
      11.  */  
      12. x = 1  
      13. println x  
      14. x = new java.util.Date()  
      15. println x  
      16. x = -3.1499392  
      17. println x  
      18. x = false  
      19. println x  
      20. x = "Hi"  
      21. println x  
      22.   
      23. myList = [1776, -133990928734928763]  
      24. println myList[0]  
      25. println myList.size()  
      26.   
      27. scores = [ "Brett":100"Pete":"Did not finish""Andrew":86.87934 ]  
      28. println scores["Pete"]  
      29. println scores.Pete   
      30. scores["Pete"] = 3  
      31. println scores.Pete  
      32.   
      33. amPM = Calendar.getInstance().get(Calendar.AM_PM)  
      34. if (amPM == Calendar.AM)  
      35. {  
      36.     println("Good morning")  
      37. } else {  
      38.     println("Good evening")  
      39. }  
      40.   
      41. square = { it * it }  
      42. println(square(9))  
      43. 1234 ].collect(square)  
      44.   
      45. printMapClosure = { key, value -> println key + "=" + value }  
      46. "yue" : "wu""lane" : "burks""sudha" : "saseethiaseeleethialeselan"].each(printMapClosure)  
      47.   
      48. fullString = ""  
      49. orderParts = ["BUY"200"Hot Dogs""1"]  
      50. orderParts.each {  
      51.     fullString += it + " "  
      52. }  
      53. println fullString  
      54.   
      55. myMap = ["asdf"1 , "qwer" : 2"sdfg" : 10]  
      56. result = 0  
      57. myMap.keySet().each( { result+= myMap[it] } )  
      58. println result  
      59.   
      60. class Class1 {  
      61.     def closure = {  
      62.         println this.class.name  
      63.         println delegate.class.name  
      64.         def nestedClos = {  
      65.             println owner.class.name  
      66.         }  
      67.         nestedClos()  
      68.     }  
      69. }  
      70. def clos = new Class1().closure  
      71. clos.delegate = this  
      72. clos()  
      73. /* prints:  
      74. Class1  
      75. Script1  
      76. Class1$_closure1 */  
      77.   
      78. def list = ['a','b','c','d']  
      79. def newList = []  
      80. list.collect( newList ) {  
      81.     it.toUpperCase()  
      82. }  
      83. println newList // ["A""B""C""D"]  
      84.   
      85. list = ['a','b','c','d']  
      86. newList = []  
      87. clos = { it.toUpperCase() }  
      88. list.collect( newList, clos )  
      89. assert newList == ["A""B""C""D"]  
      90.   
      91. class Book {  
      92.     private String title  
      93.     Book (String theTitle) {  
      94.         title = theTitle  
      95.     }  
      96.     String getTitle(){  
      97.         return title  
      98.     }  
      99. }  
      100.   
      101. class SomeClass {  
      102.     public fieldWithModifier  
      103.     String typedField  
      104.     def untypedField  
      105.     protected field1, field2, field3  
      106.     private assignedField = new Date()  
      107.     static classField  
      108.     public static final String CONSTA = 'a', CONSTB = 'b'  
      109.     def someMethod(){  
      110.         def localUntypedMethodVar = 1  
      111.         int localTypedMethodVar = 1  
      112.         def localVarWithoutAssignment, andAnotherOne  
      113.     }  
      114. }  
      115. def localvar = 1  
      116. boundvar1 = 1  
      117. def someMethod(){  
      118.     localMethodVar = 1  
      119.     boundvar2 = 1  
      120. }  
      121.   
      122. class Counter {  
      123.     public count = 0  
      124. }  
      125. def counter = new Counter()  
      126. counter.count = 1  
      127. assert counter.count == 1  
      128. def fieldName = 'count'  
      129. counter[fieldName] = 2  
      130. assert counter['count'] == 2  

       

       

      python

      Python代碼
       收藏代碼
      1. import string  
      2. __author__ = "jnotnull"  
      3. __date__ = "$2009-7-14 9:35:19$"  
      4. #coding:utf-8  
      5. print 'hello world'  
      6. print('hello world 我是jnotnull')  
      7.   
      8. i = 11   #整數(shù)類(lèi)型  
      9. d = 1.5 #浮點(diǎn)數(shù)  
      10. str = 'abc'  #字符串  
      11. a = 'a'      #單個(gè)字符  
      12. flag1 = True   #bool類(lèi)型  
      13. flag2 = False  #bool類(lèi)型  
      14.   
      15. #下面分別是乘法,除法和求余運(yùn)算  
      16. #print 可以打印多個(gè)參數(shù),每個(gè)參數(shù)中用逗號(hào)隔開(kāi)。  
      17. print i, i * 5, i / 5, i % 2  
      18. print i * d     #整數(shù)與浮點(diǎn)數(shù)相乘  
      19. print str + a   #字符串的連接  
      20.   
      21. s = '100'  
      22. s1 = '1.99'  
      23. print int(s)   #類(lèi)型轉(zhuǎn)換  
      24. print float(s1) #類(lèi)型轉(zhuǎn)換  
      25. string.atoi(s)   #解析整數(shù)  
      26. string.atof(s1)  #解釋浮點(diǎn)數(shù)  
      27.   
      28. arr = (123)      #元組,用?。▓A)括號(hào)  
      29. list = [456]     #列表,用中(方)括號(hào)  
      30. dict = {}            #詞典,用大括號(hào),一個(gè)空的詞典  
      31. dict1 = {1:'a'2:'b'}  #初始化,key是1,value是'a';key 是2,value是'b'  
      32.   
      33. print arr[0]  
      34. print list[0]  
      35. print dict1  
      36.   
      37. a = 1  
      38. if a == 1:         #注意后面有一個(gè)冒號(hào)。其中“==”是相等判斷  
      39.     print 1       #注意print 函數(shù)之前有一個(gè)tab鍵,這就是python的強(qiáng)制縮進(jìn)  
      40. else:         #注意else后面的冒號(hào)  
      41.     print 0       #注意縮進(jìn)  
      42.   
      43. if (a == 1):      #可以添加園括號(hào)  
      44.     print 1  
      45. else:  
      46.     print 0  
      47.   
      48. a = 1  
      49. b = 0  
      50. if a == 1 and b == 1:   #and 是邏輯“與”運(yùn)算,自然“or”就是邏輯“或”運(yùn)算  
      51.     print 1  
      52. else:  
      53.     print 0  
      54.   
      55. b = 0  
      56. if a == 0:  
      57.     print i  
      58.     i -= 1       #注意python不支持i--,i++,--i,++i之類(lèi)的運(yùn)算  
      59. elif b == 0:  
      60.     print  i  
      61.   
      62. #fun1的函數(shù)體為空  
      63. #需要使用pass語(yǔ)句占位,因?yàn)楹瘮?shù)體至少要有一個(gè)句  
      64. #對(duì)編寫(xiě)框架程序有用處  
      65. def fun1():  
      66.     pass  
      67.   
      68. #一個(gè)最簡(jiǎn)單的函數(shù),輸入一個(gè)數(shù),返回這個(gè)數(shù)的兩倍  
      69. def fun2(i):  
      70.     return i * 2  
      71.   
      72. #返回多個(gè)值,返回值是一個(gè)元組  
      73. def fun3(i):  
      74.     return i * 2, i / 2  
      75.   
      76. #重載,支持不同的參數(shù)類(lèi)型  
      77. def fun4(x):  
      78.     import   types   #引入一個(gè)庫(kù),可以判斷變量的類(lèi)型  
      79.     if type(x)   is   types.IntType:#判斷是否int   類(lèi)型  
      80.         return 2 * x  
      81.     if type(x)   is   types.StringType:#是否string類(lèi)型  
      82.         return x + x  
      83.   
      84.   
      85. print 'fun2:', fun2(1)  
      86. print 'fun3:', fun3(4)  
      87.   
      88. print 'fun4:', fun4(10)  
      89. print 'fun4:', fun4('abc')  
      90.   
      91. #建立一個(gè)類(lèi),類(lèi)名是A,注意A后面有一個(gè)冒號(hào)  
      92. class A:  
      93.     count = 0  
      94.     def __init__(self, name):   #構(gòu)造函數(shù),傳入?yún)?shù)是name;  
      95.         self.name = name        #self類(lèi)似java里面this關(guān)鍵字  
      96.   
      97.     def setName(self, name):    #A的一個(gè)成員函數(shù)  
      98.         self.name = name  
      99.     def getName(self):  
      100.         return self.name  
      101.   
      102. #__name__是一個(gè)系統(tǒng)變量  
      103. #當(dāng)您直接運(yùn)行模塊,__name__ 的值是 __main__;  
      104. #當(dāng)您把該文件作為一個(gè)導(dǎo)入模塊,__name__ 就是其他值  
      105. #這樣方便測(cè)試  
      106. if __name__ == "__main__":  
      107.     #初始化一個(gè)對(duì)象A  
      108.     a = A('poson')  
      109.     print a.getName()  
      110.   
      111. class HttpBase:  
      112.     def get(self):  
      113.         psss  
      114. class Http1(HttpBase):  
      115.     def get(self):  
      116.         print 'http1'  
      117. class Http2(HttpBase):  
      118.     def get(self):  
      119.         print 'http2'  
      120.   
      121.   
      122. class Base:  
      123.     def __init__(self):  
      124.         self.httpobj = None  
      125.     def http(self):  
      126.         self.httpobj.get()  
      127.     def compute(self):  
      128.         self.http()  
      129.         self.show()  
      130.     #虛函數(shù)  
      131.     def show(self):  
      132.         pass  
      133.     def notify(self, k):  
      134.         print 'notify', k  
      135.   
      136.   
      137. #橋接模式,通過(guò)A,B 關(guān)聯(lián)不同的http1和http2  
      138. class BaseA(Base):  
      139.     def __init__(self):  
      140.         self.httpobj = Http1()  
      141.     def notify(self, k):  
      142.         print 'A notify', k  
      143.     def show(self):  
      144.         print 'show a'  
      145.   
      146. class BaseB(Base):  
      147.     def __init__(self):  
      148.         self.httpobj = Http2()  
      149.     def notify(self, k):  
      150.         print 'B notify', k  
      151.     def show(self):  
      152.         print 'show b'  
      153.   
      154. #觀測(cè)者模式  
      155. class Observer:  
      156.     def __init__(self):  
      157.         self.listOB = []  
      158.     def register(self, obj):  
      159.         self.listOB.append(obj)  
      160.     def notify(self):  
      161.         for obj in self.listOB:  
      162.             obj.notify(len(self.listOB))  
      163.   
      164. #適配器模式  
      165. class B1:  
      166.     def http(self):  
      167.         BaseB().http()  
      168. #工廠模式  
      169. class Factory:  
      170.     def CreateA(self):  
      171.         return BaseA()  
      172.     def CreateB(self):  
      173.         return BaseB()  
      174.   
      175.   
      176. #單例模式  
      177. class Logger(object):  
      178.     log = None  
      179.     @staticmethod  
      180.     def new():  
      181.         import threading  
      182.         #線程安全  
      183.         mylock = threading.RLock()  
      184.         mylock.acquire()  
      185.         if not Logger.log:  
      186.             Logger.log = Logger()  
      187.         mylock.release()  
      188.   
      189.         return Logger.log  
      190.     def write(self, v):  
      191.         print 'Logger ', v  
      192.   
      193. if __name__ == "__main__":  
      194.     a = Factory().CreateA()  
      195.     b = Factory().CreateB()  
      196.   
      197.     objS = Observer()  
      198.     objS.register(a)  
      199.     objS.register(b)  
      200.   
      201.     a.compute()  
      202.     b.compute()  
      203.     objS.notify()  
      204.   
      205.     b1 = B1()  
      206.     b1.http()  
      207.   
      208.     Logger.new().log.write('v')    

       

       

      參考資料http://poson.

      http://openmouse.

        本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶(hù)發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買(mǎi)等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
        轉(zhuǎn)藏 分享 獻(xiàn)花(0

        0條評(píng)論

        發(fā)表

        請(qǐng)遵守用戶(hù) 評(píng)論公約

        類(lèi)似文章 更多