您现在的位置: 万盛学电脑网 >> 程序编程 >> 网络编程 >> 编程语言综合 >> 正文

进一步深入Ruby中的类与对象概念

作者:佚名    责任编辑:admin    更新时间:2022-06-22

   这篇文章主要介绍了进一步深入Ruby中的类与对象概念,包括集成与多态等更多知识点的整理,需要的朋友可以参考下

  Ruby是纯面向对象的语言,所有项目似乎要Ruby中为一个对象。Ruby中的每个值是一个对象,即使是最原始的东西:字符串,数字甚至true和false。即使是一个类本身是一个对象,它是Class类的一个实例。本章将通过所有功能涉及到Ruby的面向对象。

  类是用来指定对象的形式,它结合了数据表示和方法操纵这些数据,转换成一个整齐的包。在一个类的数据和方法,被称为类的成员。

  Ruby类的定义:

  定义一个类,定义的数据类型的草图。 这实际上并不定义任何数据,但它定义的类名字的意思什么,即是什么类的对象将包括这样一个对象上执行什么操作可以。

  类定义开始与关键字class类名和 end 分隔。例如,我们定义Box类使用class关键字如下:

  class Box

  code

  end

  名称必须以大写字母开始,按照约定名称中包含多个单词,每个单词没有分隔符(驼峰式)一起执行。

  定义Ruby的对象:

  类为对象的蓝图,所以基本上是一个从一个类对象被创建。我们声明一个类的对象使用new关键字。下面的语句声明了两个对象,Box 类:

  ?

1 2 box1 = Box.new box2 = Box.new

  initialize方法:

  initialize方法是一个标准的Ruby类的方法,和其它面向对象编程语言的构造方法有相同的方式工作。 initialize方法是有用的,在创建对象的时候,一些类变量初始化。这种方法可能需要的参数列表,它像其他Ruby之前的方法用def关键字定义,如下所示:

  class Box

  def initialize(w,h)

  @width, @height = w, h

  end

  end

  实例变量:

  实例变量是类的一种属性,一旦我们使用的类对象被创建的对象的属性。每个对象的属性被分别赋值的并与其它对象共享,它们在类的内部使用@操作符访问,但访问类之外的,我们使用的公共方法被称为访问器方法。如果我们把上述定义的类 Box,然后 @width 和 @height 类 Box实例变量。

  ?

1 2 3 4 5 6 class Box def initialize(w,h) # assign instance avriables @width, @height = w, h end end

  访问器和setter方法:

  为了外部能访问类的变量,它们必须定义存取器方法,这些存取器方法也被称为getter方法。下面的例子演示了如何使用访问器方法:

  ?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 #!/usr/bin/ruby -w   # define a class class Box # constructor method def initialize(w,h) @width, @height = w, h end   # accessor methods def printWidth @width end   def printHeight @height end end   # create an object box = Box.new(10, 20)   # use accessor methods x = box.printWidth() y = box.printHeight()   puts "Width of the box is : #{x}" puts "Height of the box is : #{y}"

  当上面的代码执行时,它会产生以下结果:

  ?

1 2 Width of the box is : 10 Height of the box is : 20

  类似的存取方法用于访问的变量值,Ruby提供了一种方法来从类的外部设置这些变量的值,那就是setter方法??,定义如下:

  ?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 #!/usr/bin/ruby -w   # define a class class Box # constructor method def initialize(w,h) @width, @height = w, h end   # accessor methods def getWidth @width end def getHeight @height end   # setter methods def setWidth=(value) @width = value end def setHeight=(value) @height = value end end   # create an object box = Box.new(10, 20)   # use setter methods box.setWidth = 30 box.setHeight = 50   # use accessor methods x = box.getWidth() y = box.getHeight()   puts "Width of the box is : #{x}" puts "Height of the box is : #{y}"

  当上面的代码执行时,它会产生以下结果:

  ?

1 2 Width of the box is : 30 Height of the box is : 50

  实例方法:

  也以同样的方式,因为我们使用def关键字定义其他方法,并按下图所示仅对使用一个类的实例,它们可以被用来定义该实例方法。它们的功能不局限于访问实例变量,他们也可以按要求做更多的事情。

  ?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 #!/usr/bin/ruby -w   # define a class class Box # constructor method def initialize(w,h) @width, @height = w, h end # instance method def getArea @width * @height end end   # create an object box = Box.new(10, 20)   # call instance methods a = box.getArea() puts "Area of the box is : #{a}"

  当上面的代码执行时,它会产生以下结果:

  ?

1 Area of the box is : 200

  类的方法和变量:

  类变量是一个变量,这是一个类