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

Ruby的面向对象编程的基础教程

程序员文章站 2022-11-15 20:08:50
ruby 是纯面向对象的语言,ruby 中的一切都是以对象的形式出现。ruby 中的每个值都是一个对象,即使是最原始的东西:字符串、数字,甚至连 true 和 false...

ruby 是纯面向对象的语言,ruby 中的一切都是以对象的形式出现。ruby 中的每个值都是一个对象,即使是最原始的东西:字符串、数字,甚至连 true 和 false 都是对象。类本身也是一个对象,是 class 类的一个实例。本章将向您讲解所有与 ruby 面向对象相关的主要功能。

类用于指定对象的形式,它结合了数据表示法和方法,把数据整理成一个整齐的包。类中的数据和方法被称为类的成员。
ruby 类定义

当您定义一个类时,您实际是定义了一个数据类型的蓝图。这实际上并没有定义任何的数据,而是定义了类的名称意味着什么,也就是说,定义了类的对象将由什么组成,以及在该对象上能执行什么操作。

类定义以关键字 class 开始,后跟类名称,最后以一个 end 进行分隔表示终止该类定义。例如,我们使用关键字 class 来定义 box 类,如下所示:

class box
  code
end

按照惯例,名称必须以大写字母开头,如果包含多个单词,每个单词首字母大写,但此间没有分隔符(例如:camelcase)。
定义 ruby 对象

类提供了对象的蓝图,所以基本上,对象是根据类进行创建的。我们使用 new 关键字声明类的对象。下面的语句声明了类 box 的两个对象:

box1 = box.new
box2 = box.new

initialize 方法

initialize 方法是一个标准的 ruby 类方法,与其他面向对象编程语言中的 constructor 工作原理类似。当您想要在创建对象的同时初始化一些类变量,initialize 方法就派上用场了。该方法带有一系列参数,与其他 ruby 方法一样,使用该方法时,必须在前面放置 def 关键字,如下所示:

class box
  def initialize(w,h)
   @width, @height = w, h
  end
end

实例变量

实例变量是类属性,它们在使用类创建对象时就变成对象的属性。每个对象的属性是单独赋值的,和其他对象之间不共享值。在类的内部,是使用 @ 运算符访问这些属性,在类的外部,则是使用称为访问器方法的公共方法进行访问。下面我们以上面定义的类 box 为实例,把 @width 和 @height 作为类 box 的实例变量。

class box
  def initialize(w,h)
   # 给实例变量赋值
   @width, @height = w, h
  end
end

访问器 & 设置器 方法

为了在类的外部使用变量,我们必须在访问器方法内部定义这些变量,这些访问器方法也被称为获取器方法。下面的实例演示了访问器方法的用法:

#!/usr/bin/ruby -w
 
# 定义类
class box
  # 构造器方法
  def initialize(w,h)
   @width, @height = w, h
  end
 
  # 访问器方法
  def printwidth
   @width
  end
 
  def printheight
   @height
  end
end
 
# 创建对象
box = box.new(10, 20)
 
# 使用访问器方法
x = box.printwidth()
y = box.printheight()
 
puts "width of the box is : #{x}"
puts "height of the box is : #{y}"

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

width of the box is : 10
height of the box is : 20

与用于访问变量值的访问器方法类似,ruby 提供了一种在类的外部设置变量值的方式,也就是所谓的设置器方法,定义如下:

#!/usr/bin/ruby -w
 
# 定义类
class box
  # 构造器方法
  def initialize(w,h)
   @width, @height = w, h
  end
 
  # 访问器方法
  def getwidth
   @width
  end
  def getheight
   @height
  end
 
  # 设置器方法
  def setwidth=(value)
   @width = value
  end
  def setheight=(value)
   @height = value
  end
end
 
# 创建对象
box = box.new(10, 20)
 
# 使用设置器方法
box.setwidth = 30
box.setheight = 50
 
# 使用访问器方法
x = box.getwidth()
y = box.getheight()
 
puts "width of the box is : #{x}"
puts "height of the box is : #{y}"

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

width of the box is : 30
height of the box is : 50

实例方法

实例方法的定义与其他方法的定义一样,都是使用 def 关键字,但它们只能通过类实例来使用,如下面实例所示。它们的功能不限于访问实例变量,也能按照您的需求做更多其他的任务。

#!/usr/bin/ruby -w
 
# 定义类
class box
  # constructor method
  def initialize(w,h)
   @width, @height = w, h
  end
  # 实例方法
  def getarea
   @width * @height
  end
end
 
# 创建对象
box = box.new(10, 20)
 
# 调用实例方法
a = box.getarea()
puts "area of the box is : #{a}"

当上面的代码执行时,它会产生以下结果:
area of the box is : 200
类方法 & 类变量

类变量是在类的所有实例*享的变量。换句话说,类变量的实例可以被所有的对象实例访问。类变量以两个 @ 字符(@@)作为前缀,类变量必须在类定义中被初始化,如下面实例所示。

类方法使用 def self.methodname() 定义,类方法以 end 分隔符结尾。类方法可使用带有类名称的 classname.methodname 形式调用,如下面实例所示:
#!/usr/bin/ruby -w
 
class box
  # 初始化类变量
  @@count = 0
  def initialize(w,h)
   # 给实例变量赋值
   @width, @height = w, h
 
   @@count += 1
  end
 
  def self.printcount()
   puts "box count is : #@@count"
  end
end
 
# 创建两个对象
box1 = box.new(10, 20)
box2 = box.new(30, 100)
 
# 调用类方法来输出盒子计数
box.printcount()

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

box count is : 2

to_s 方法

您定义的任何类都有一个 to_s 实例方法来返回对象的字符串表示形式。下面是一个简单的实例,根据 width 和 height 表示 box 对象:

#!/usr/bin/ruby -w
 
class box
  # 构造器方法
  def initialize(w,h)
   @width, @height = w, h
  end
  # 定义 to_s 方法
  def to_s
   "(w:#@width,h:#@height)" # 对象的字符串格式
  end
end
 
# 创建对象
box = box.new(10, 20)
 
# 自动调用 to_s 方法
puts "string representation of box is : #{box}"

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

string representation of box is : (w:10,h:20)

访问控制

ruby 为您提供了三个级别的实例方法保护,分别是 public、private 或 protected。ruby 不在实例和类变量上应用任何访问控制。

  •     public 方法: public 方法可被任意对象调用。默认情况下,方法都是 public 的,除了 initialize 方法总是 private 的。
  •     private 方法: private 方法不能从类外部访问或查看。只有类方法可以访问私有成员。
  •     protected 方法: protected 方法只能被类及其子类的对象调用。访问也只能在类及其子类内部进行。

下面是一个简单的实例,演示了这三种修饰符的语法:

#!/usr/bin/ruby -w
 
# 定义类
class box
  # 构造器方法
  def initialize(w,h)
   @width, @height = w, h
  end
 
  # 实例方法默认是 public 的
  def getarea
   getwidth() * getheight
  end
 
  # 定义 private 的访问器方法
  def getwidth
   @width
  end
  def getheight
   @height
  end
  # make them private
  private :getwidth, :getheight
 
  # 用于输出面积的实例方法
  def printarea
   @area = getwidth() * getheight
   puts "big box area is : #@area"
  end
  # 让实例方法是 protected 的
  protected :printarea
end
 
# 创建对象
box = box.new(10, 20)
 
# 调用实例方法
a = box.getarea()
puts "area of the box is : #{a}"
 
# 尝试调用 protected 的实例方法
box.printarea()

当上面的代码执行时,它会产生以下结果。在这里,第一种方法调用成功,但是第二方法会产生一个问题。

area of the box is : 200
test.rb:42: protected method `printarea' called for #
<box:0xb7f11280 @height=20, @width=10> (nomethoderror)

类的继承

继承,是面向对象编程中最重要的概念之一。继承允许我们根据另一个类定义一个类,这样使得创建和维护应用程序变得更加容易。

继承有助于重用代码和快速执行,不幸的是,ruby 不支持多继承,但是 ruby 支持 mixins。mixin 就像是多继承的一个特定实现,在多继承中,只有接口部分是可继承的。

当创建类时,程序员可以直接指定新类继承自某个已有类的成员,这样就不用从头编写新的数据成员和成员函数。这个已有类被称为基类或父类,新类被称为派生类或子类。

ruby 也提供了子类化的概念,子类化即继承,下面的实例解释了这个概念。扩展一个类的语法非常简单。只要添加一个 < 字符和父类的名称到类语句中即可。例如,下面定义了类 bigbox 是 box 的子类:

#!/usr/bin/ruby -w
 
# 定义类
class box
  # 构造器方法
  def initialize(w,h)
   @width, @height = w, h
  end
  # 实例方法
  def getarea
   @width * @height
  end
end
 
# 定义子类
class bigbox < box
 
  # 添加一个新的实例方法
  def printarea
   @area = @width * @height
   puts "big box area is : #@area"
  end
end
 
# 创建对象
box = bigbox.new(10, 20)
 
# 输出面积
box.printarea()

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

big box area is : 200

方法重载

虽然您可以在派生类中添加新的功能,但有时您可能想要改变已经在父类中定义的方法的行为。这时您可以保持方法名称不变,重载方法的功能即可,如下面实例所示:

#!/usr/bin/ruby -w
 
# 定义类
class box
  # 构造器方法
  def initialize(w,h)
   @width, @height = w, h
  end
  # 实例方法
  def getarea
   @width * @height
  end
end
 
# 定义子类
class bigbox < box
 
  # 改变已有的 getarea 方法
  def getarea
   @area = @width * @height
   puts "big box area is : #@area"
  end
end
 
# 创建对象
box = bigbox.new(10, 20)
 
# 使用重载的方法输出面积
box.getarea()

运算符重载

我们希望使用 + 运算符执行两个 box 对象的向量加法,使用 * 运算符来把 box 的 width 和 height 相乘,使用一元运算符 - 对 box 的 width 和 height 求反。下面是一个带有数学运算符定义的 box 类版本:

class box
 def initialize(w,h) # 初始化 width 和 height
  @width,@height = w, h
 end
 
 def +(other)     # 定义 + 来执行向量加法
  box.new(@width + other.width, @height + other.height)
 end
 
 def -@        # 定义一元运算符 - 来对 width 和 height 求反
  box.new(-@width, -@height)
 end
 
 def *(scalar)    # 执行标量乘法
  box.new(@width*scalar, @height*scalar)
 end
end

冻结对象

有时候,我们想要防止对象被改变。在 object 中,freeze 方法可实现这点,它能有效地把一个对象变成一个常量。任何对象都可以通过调用 object.freeze 进行冻结。冻结对象不能被修改,也就是说,您不能改变它的实例变量。

您可以使用 object.frozen? 方法检查一个给定的对象是否已经被冻结。如果对象已被冻结,该方法将返回 true,否则返回一个 false 值。下面的实例解释了这个概念:

#!/usr/bin/ruby -w
 
# 定义类
class box
  # 构造器方法
  def initialize(w,h)
   @width, @height = w, h
  end
 
  # 访问器方法
  def getwidth
   @width
  end
  def getheight
   @height
  end
 
  # 设置器方法
  def setwidth=(value)
   @width = value
  end
  def setheight=(value)
   @height = value
  end
end
 
# 创建对象
box = box.new(10, 20)
 
# 让我们冻结该对象
box.freeze
if( box.frozen? )
  puts "box object is frozen object"
else
  puts "box object is normal object"
end
 
# 现在尝试使用设置器方法
box.setwidth = 30
box.setheight = 50
 
# 使用访问器方法
x = box.getwidth()
y = box.getheight()
 
puts "width of the box is : #{x}"
puts "height of the box is : #{y}"

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

box object is frozen object
test.rb:20:in `setwidth=': can't modify frozen object (typeerror)
    from test.rb:39

类常量

您可以在类的内部定义一个常量,通过把一个直接的数值或字符串值赋给一个变量来定义的,常量的定义不需要使用 @ 或 @@。按照惯例,常量的名称使用大写。

一旦常量被定义,您就不能改变它的值,您可以在类的内部直接访问常量,就像是访问变量一样,但是如果您想要在类的外部访问常量,那么您必须使用 classname::constant,如下面实例所示。

#!/usr/bin/ruby -w
 
# 定义类
class box
  box_company = "tata inc"
  boxweight = 10
  # 构造器方法
  def initialize(w,h)
   @width, @height = w, h
  end
  # 实例方法
  def getarea
   @width * @height
  end
end
 
# 创建对象
box = box.new(10, 20)
 
# 调用实例方法
a = box.getarea()
puts "area of the box is : #{a}"
puts box::box_company
puts "box weight is: #{box::boxweight}"

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

area of the box is : 200
tata inc
box weight is: 10

类常量可被继承,也可像实例方法一样被重载。
使用 allocate 创建对象

可能有一种情况,您想要在不调用对象构造器 initialize 的情况下创建对象,即,使用 new 方法创建对象,在这种情况下,您可以调用 allocate 来创建一个未初始化的对象,如下面实例所示:

#!/usr/bin/ruby -w
 
# 定义类
class box
  attr_accessor :width, :height
 
  # 构造器方法
  def initialize(w,h)
   @width, @height = w, h
  end
 
  # 实例方法
  def getarea
   @width * @height
  end
end
 
# 使用 new 创建对象
box1 = box.new(10, 20)
 
# 使用 allocate 创建两一个对象
box2 = box.allocate
 
# 使用 box1 调用实例方法
a = box1.getarea()
puts "area of the box is : #{a}"
 
# 使用 box2 调用实例方法
a = box2.getarea()
puts "area of the box is : #{a}"

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

area of the box is : 200
test.rb:14: warning: instance variable @width not initialized
test.rb:14: warning: instance variable @height not initialized
test.rb:14:in `getarea': undefined method `*'
  for nil:nilclass (nomethoderror) from test.rb:29

类信息

如果类定义是可执行代码,这意味着,它们可在某个对象的上下文中执行,self 必须引用一些东西。让我们来看看下面的实例:.

#!/usr/bin/ruby -w
 
class box
  # 输出类信息
  puts "type of self = #{self.type}"
  puts "name of self = #{self.name}"
end

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

type of self = class
name of self = box

这意味着类定义可通过把该类作为当前对象来执行,同时也意味着元类和父类中的该方法在方法定义执行期间是可用的。