欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Ruby语言快速入门之对象,方法和类 RubySmalltalkWindows 

程序员文章站 2023-12-21 11:35:46
...
喜欢使用高度面向对象的<nobr oncontextmenu="return false;" onmousemove="kwM(2);" id="key2" onmouseover="kwE(event,2, this);" style="COLOR: #6600ff; BORDER-BOTTOM: #6600ff 1px dotted; BACKGROUND-COLOR: transparent; TEXT-DECORATION: underline" onclick="return kwC();" onmouseout="kwL(event, this);" target="_blank">语言</nobr>(例如Smalltalk,Eiffel或CLOS)的用户来说,这是非常受欢迎的。例如1,2,3或10.8等等都是对象,而不是如JavaC++中的原始类型;字符串是对象,类和<nobr oncontextmenu="return false;" onmousemove="kwM(6);" id="key5" onmouseover="kwE(event,6, this);" style="COLOR: #6600ff; BORDER-BOTTOM: 0px dotted; BACKGROUND-COLOR: transparent; TEXT-DECORATION: underline" onclick="return kwC();" onmouseout="kwL(event, this);" target="_blank">方法</nobr>也都是对象。例如,下面都是有效的Ruby代码(在Ruby中,注释行是以"#"符号界定的):

#对象-34的绝对值
-34.abs
#对一个浮点数进行四舍五入处理
10.8.round
#返回一个字符串对象的大写且逆转的副本
"This is Ruby".upcase.reverse
#返回数学sin方法的参数个数
Math.method(:sin).arity

Ruby语言快速入门之对象,方法和类
            
    
    
        RubySmalltalkWindows 
图5.Ruby是全对象化的:在Ruby中,整数,浮点数,字符串,甚至类和方法都是对象。这里的代码<nobr oncontextmenu="return false;" onmousemove="kwM(4);" id="key3" onmouseover="kwE(event,4, this);" style="COLOR: #6600ff; BORDER-BOTTOM: #6600ff 1px dotted; BACKGROUND-COLOR: transparent; TEXT-DECORATION: underline" onclick="return kwC();" onmouseout="kwL(event, this);" target="_blank">展示</nobr>了针对这些类型对象的方法调用。


  在Ruby中,所有功能都是通过调用对象上的方法(或操作)实现的。事实上,Ruby中的方法调用就象其它<nobr oncontextmenu="return false;" onmousemove="kwM(1);" id="key1" onmouseover="kwE(event,1, this);" style="COLOR: #6600ff; BORDER-BOTTOM: #6600ff 1px dotted; BACKGROUND-COLOR: transparent; TEXT-DECORATION: underline" onclick="return kwC();" onmouseout="kwL(event, this);" target="_blank">程序</nobr>语言中的函数或过程调用一样。

  就象在所有面向对象程序语言中一样,对象是从类中创建的。Ruby库中提供了许多预构建的类。你可以修改这些类或构建你自己的类。Ruby中的类是使用"class"关键字定义的。类名<nobr oncontextmenu="return false;" onmousemove="kwM(0);" id="key0" onmouseover="kwE(event,0, this);" style="COLOR: #6600ff; BORDER-BOTTOM: #6600ff 1px dotted; BACKGROUND-COLOR: transparent; TEXT-DECORATION: underline" onclick="return kwC();" onmouseout="kwL(event, this);" target="_blank">开始</nobr>是一个大写字母。类定义以"end"关键字结束。因此,一个Rectangle类的定义可能有如下形式:

class Rectangle
end

  为了把方法添加到类,可以使用def关键字。方法的定义也应该以end关键字结束。跟随def关键字和方法名后面就是方法参数。把一个area方法添加到上面的Rectangle类的代码看上去如下所示:

class Rectangle
def area (hgt,wdth)
return hgt*wdth
end
end

  对于那些熟悉其它程序语言的用户,他可能注意到一些差别。Ruby并不使用任何花括号来限定类或方法,也不使用分号或其它字符来表示程序语句行的结束。Ruby的目标,根据它的创建者说明,是简单、易用并使编码成为一件"趣事"。谁想记住所有的那些分号?没有意思!在Ruby中,只要你把语句放在一行上,不需要分号或其它代码行结束标记。顺便说一下,在area方法参数周围的括号是不必要的。在默认情况下,Ruby返回一个方法中最后的内容,因此return关键字也可以省略。因此,你可以建立如下简单编码的Rectangle类:

class Rectangle
def area hgt, wdth
hgt*wdth
end
end


  尽管上面代码是有效的,但是小括号还是被<nobr oncontextmenu="return false;" onmousemove="kwM(5);" id="key4" onmouseover="kwE(event,5, this);" style="COLOR: #6600ff; BORDER-BOTTOM: #6600ff 1px dotted; BACKGROUND-COLOR: transparent; TEXT-DECORATION: underline" onclick="return kwC();" onmouseout="kwL(event, this);" target="_blank">推荐</nobr>使用于方法参数<nobr oncontextmenu="return false;" onmousemove="kwM(7);" id="key6" onmouseover="kwE(event,7, this);" style="COLOR: #6600ff; BORDER-BOTTOM: #6600ff 1px dotted; BACKGROUND-COLOR: transparent; TEXT-DECORATION: underline" onclick="return kwC();" onmouseout="kwL(event, this);" target="_blank">表达</nobr>的,这主要是为了实现较好的可读性。

(在一些<nobr oncontextmenu="return false;" onmousemove="kwM(3);" id="key3" onmouseover="kwE(event,3, this);" style="COLOR: #6600ff; BORDER-BOTTOM: #6600ff 1px dotted; BACKGROUND-COLOR: transparent; TEXT-DECORATION: underline" onclick="return kwC();" onmouseout="kwL(event, this);" target="_blank">语言</nobr>中也称为属性)。例如,由Rectangle类创建的对象应该都有一个高度和宽度。在Ruby中,实例变量不必显式地在类中声明,只是必须在它们的命名中以一个<nobr oncontextmenu="return false;" onmousemove="kwM(7);" id="key6" onmouseover="kwE(event,7, this);" style="COLOR: #6600ff; BORDER-BOTTOM: #6600ff 1px dotted; BACKGROUND-COLOR: transparent; TEXT-DECORATION: underline" onclick="return kwC();" onmouseout="kwL(event, this);" target="_blank">特殊字符</nobr>来标记和使用。具体地说,所有的实例变量名都以"@"开头。为了实现当调用area<nobr oncontextmenu="return false;" onmousemove="kwM(2);" id="key2" onmouseover="kwE(event,2, this);" style="COLOR: #6600ff; BORDER-BOTTOM: #6600ff 1px dotted; BACKGROUND-COLOR: transparent; TEXT-DECORATION: underline" onclick="return kwC();" onmouseout="kwL(event, this);" target="_blank">方法</nobr>时,存储矩形实例的高度和宽度,你仅需把实例变量添加到area方法即可:

class Rectangle
def area (hgt, wdth)
@height=hgt
@width = wdth
@height*@width
end
end

  更确切地说,当创建一个Rectangle实例时,应该指定高度和宽度,而实例变量在此时才确定。另外,Ruby提供了一种特殊的方法initialize,它允许你建立或准备类的新实例:

class Rectangle
def initialize (hgt, wdth)
@height = hgt
@width = wdth
end
def area ()
@height*@width
end
end

  为了创建一个新的Rectangle对象或实例,你要调用<nobr oncontextmenu="return false;" onmousemove="kwM(4);" id="key4" onmouseover="kwE(event,4, this);" style="COLOR: #6600ff; BORDER-BOTTOM: #6600ff 1px dotted; BACKGROUND-COLOR: transparent; TEXT-DECORATION: underline" onclick="return kwC();" onmouseout="kwL(event, this);" target="_blank">标准</nobr>的Ruby类构造器方法"new":

Rectangle.new(4,7)

  或,你可以使用没有括号的形式:

Rectangle.new 4,7

  这个例子创建了一个新的Rectangle对象并且调用了initialize方法,其中传入参数4和7。注意,在下面的代码中添加了height和width方法以便共享高度和宽度信息:

class Rectangle
def initialize (hgt, wdth)
@height = hgt
@width = wdth
end
def height
return @height
end
def width
return @width
end
def area ()
@height*@width
end
end

  同样,为了使另外某个方法能够更新或<nobr oncontextmenu="return false;" onmousemove="kwM(5);" id="key5" onmouseover="kwE(event,5, this);" style="COLOR: #6600ff; BORDER-BOTTOM: #6600ff 1px dotted; BACKGROUND-COLOR: transparent; TEXT-DECORATION: underline" onclick="return kwC();" onmouseout="kwL(event, this);" target="_blank">设置</nobr>一个Rectangle对象的高度和宽度,需要定义其它一些设置方法:

class Rectangle
def initialize (hgt, wdth)
@height = hgt
@width = wdth
end
def height
return @height
end
def height=(newHgt)
@height=newHgt
end
def width
return @width
end
def width=(newWdth)
@width=newWdth
end
def area ()
@height*@width
end
end

  译者注 本文中的mutator和accessor相当于其它语言中的setter和getter。

  上面的mutator方法("height="和"width=")可能看起来有点神秘,但是它们确实只是一些方法。不要让命名中的等号蒙骗了你。在方法名最后的额外字符对于Ruby并不意味着什么,但是它<nobr oncontextmenu="return false;" onmousemove="kwM(1);" id="key1" onmouseover="kwE(event,1, this);" style="COLOR: #6600ff; BORDER-BOTTOM: #6600ff 1px dotted; BACKGROUND-COLOR: transparent; TEXT-DECORATION: underline" onclick="return kwC();" onmouseout="kwL(event, this);" target="_blank">提高</nobr>了代码的可读性。请看下列代码:

aRectangle.height=27

  在此,一个Rectangle对象的高度正被赋值(改变)。事实上,这仅是一个对Rectangle对象的"height="方法的调用。

  因为授予到一个对象的实例变量的存取权限非常普通,所以Ruby提供了一组关键字来实现一次性定义实例变量和accessor/mutator方法,从而使这些实例变量成为"public"属性。这些关键字是attr_reader,attr_accessor。它们有助于极大地简化代码:

class Rectangle
attr_accessor :height, :width
def initialize (hgt, wdth)
@height = hgt
@width = wdth
end
def area ()
@height*@width
end
end

  在上面的示例代码中,attr_accessor给出了Rectangle相应于height和width属性的getter和setter。

用交互式Ruby构建应用程序

  现在,你已经知道如何构建一个简单的Ruby应用程序。为了展示Ruby解释器的交互性,让我们启动一个交互的Ruby帮助和控制台工具(使用Ruby安装的fxri工具),见图6。

Ruby语言快速入门之对象,方法和类
            
    
    
        RubySmalltalkWindows 
图6.启动fxri:从Windows开始菜单中打开交互式Ruby工具fxri。

  在窗口的右下方是一个交互式的Ruby命令提示符(即"irb(main):001:0>"),它在窗口打开时显示出来。你可以进入到irb命令提示符行中并输入"Rectangle.new(6,5).area()"。之后,你应该看到如下结果:

irb(main):013:0> Rectangle.new(6,5).area()
=> 30

  简洁有力!

  在Ruby中,类从来不关闭。这意味着,你总是可以在一个现有类上添加或重定义方法。例如,你还可以在你创建的Rectangle类上添加一个circumference方法。在命令提示符上,请逐行输入下列代码:

class Rectangle
def circumference ()
@height * 2 + @width * 2
end
end

Ruby语言快速入门之对象,方法和类
            
    
    
        RubySmalltalkWindows 
图7.输入Rectangle类:把Rectangle类的定义输入到fxri交互式Ruby解释器中,见图中的右下方。

  Rectangle类又被定义了一次?不,Ruby解释器知道你正在修改当前的Rectangle类,它把一个新方法添加到现有Rectangle类中。现在,在命令提示符上输入下列一行:"Rectangle.new(2,3).circumference()"。你应该看到类似如下的结果:

irb(main):014:0> class Rectangle
irb(main):015:1> def circumference()
irb(main):016:2> @height * 2 + @width * 2
irb(main):017:2> end
irb(main):018:1> end
=> nil
irb(main):019:0> Rectangle.new(2,3).circumference
=> 10

  为了重新定义Rectangle类中的任何方法,例如area方法,只需简单地重新输入具有新的area方法定义的类定义即可:

irb(main):020:0> class Rectangle
irb(main):021:1> def area()
irb(main):022:2> @height*2
irb(main):023:2> end
irb(main):024:1> end
=> nil
irb(main):025:0> Rectangle.new(6,5).area
=> 12

  在上面的简单例子中,area方法被重新定义以便总是返回原来高度的2倍。

  一个类永远不会被关闭的思想可以应用于你自己定义的类和该语言中的内嵌类中。为了说明问题,让我们把一个area方法添加到String类。在命令提示符中输入下列代码:

class String
def area()
length()
end
end

  现在,你在定义一个字符串的"area"方法以返回该字符串的长度。现在,你可以把你的名字作为一个字符串来试用一下这个新方法,见下面代码:

irb(main):026:0> class String
irb(main):027:1> def area()
irb(main):028:2> length()
irb(main):029:2> end
irb(main):030:1> end
=> nil
irb(main):031:0> "Jim".area
=> 3

  在本文示例中,我们使用Ruby的交互式特性及其开发环境来测试这种语言,而且我们仅使用了较小的代码片断。

上一篇:

下一篇: