10.3.4. ruby实例¶
1# -*- coding: utf-8 -*-
2class Gordon
3 attr_accessor :names # 让g.name可以使用
4 def initialize(name="gordon")
5 @name = name
6 end
7 def hi
8 puts "hi #{@name}"
9 end
10
11end
12
13
14g= Gordon.new
15g.hi # hi gordon
16h = Gordon.new("David")
17h.hi # hi David
18
19g.name = "miya"
20g.hi # hi miya
21puts "hi #{g.name}" # hi miya