ruby 筆記之五

2021-05-22 03:09:48 字數 1367 閱讀 9278

1.

class person

def initialize

puts "nijhao"

enddef talk (name)

puts "your name is # "

end   

end=begin

p = person.new

p.talk("samba")

=end

class student < person

def talk (name)

super

puts "your name is #"

end   

end

p1 = person.new

p1.talk("gjhohj")

p2 = student.new

p2.talk("samba")

2.#例項變數、類變數、類方法

#與全域性變數和例項變數不同,類變數在使用前必須要初始化;全域性變數和例項變數如果沒有初始化,其值為 nil 。

class student

@@count = 0 ;  #類變數

def initialize(name)

@name = name

@@count += 1

enddef talk

puts   "name=  #@name,this is class count  #@@count"

endend

p = student.new("samba")

p1 = student.new("gjhohj")

p1.talk

p.talk

3.#static class

class student

@@count = 0

def initialize

@@count += 1

enddef student.classcount

puts "class's count  #@@count"

endend

p = student.new

p1 = student.new

student.classcount

4.#例項方法

class person

def talk

puts "hello" 

endend

p1 = person.new

p2 = person.new

def p2.talk

puts "ps hello" 

enddef p2.laugh

puts "he he he!!!!"

endp1.talk

p2.talk

p2.laugh

ruby 筆記之六

1.public protected private class person private def talk puts nihao endend p1 person.new p1.talk class person public talk endp1.talk 2.第七章開始 puts math...

Ruby初學筆記之Hello World

安裝了個ruby193,寫了第乙個ruby程式,來記錄下自己學習點滴。首先在ruby的新建個資料夾sample,再新新增個檔案,名字hwww.cppcns.comelloworld.rb 內容就是 複製 如下 puts hello,world 當然也可以先宣告變數,如後再用puts語句 複製 如下 ...

ruby學習筆記之陣列學習(一)

陣列 方括號之之間放一組元素,索引從零開始。如果這個位置上沒有物件,則返回nil表示空。使用負整數訪問陣列,則從陣列末端開始計數。一 陣列定義 a 3.14159,pai 99 puts a.class puts a.length puts a 0 輸出 array 33.14159 b array...