• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell
您的位置:首页 > 脚本语言 >ruby > 进一步深入Ruby中的类与对象概念

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

作者:goldensun 字体:[增加 减小] 来源:互联网

goldensun 通过本文主要向大家介绍了ruby,ruby碧玺,ruby woo口红,mac ruby woo口红,ruby教程等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

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

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

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

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

class Box
   code
end

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

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

box1 = Box.new
box2 = Box.new

</div>

initialize方法:

initialize方法是一个标准的Ruby类的方法,和其它面向对象编程语言的构造方法有相同的方式工作。 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)
   # assign instance avriables
   @width, @height = w, h
  end
end

</div>

访问器和setter方法:

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

#!/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}"

</div>

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

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

</div>

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

#!/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}"

</div>

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

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

</div>

实例方法:

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

#!/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}"

</div>

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

Area of the box is : 200

</div>

类的方法和变量:

类变量是一个变量,这是一个类的所有实例之间共享。该变量是一个实例,它是可访问对象实例。两个@字符类变量带有前缀(@@)。在类定义类变量必须初始化,如下所示。

类方法的定义使用:def self.methodname() 以 end 字符结束,将被称为使用classname.methodname类名,在下面的例子所示:

#!/usr/bin/ruby -w

class Box
  # Initialize our class variables
  @@count = 0
  def initialize(w,h)
   # assign instance avriables
   @width, @height = w, h

   @@count += 1
  end

  def self.printCount()
   puts "Box count is : #@@count"
  end
end

# create two object
box1 = Box.new(10, 20)
box2 = Box.new(30, 100)

# call class method to print box count
Box.printCount()

</div>

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

Box count is : 2

</div>

 to_s 方法:

所定义的任何类的实例应该有一个 to_s 方法返回一个字符串形式表示对象。下面以一个简单的例子来表示一个Box对象,在宽度和高度方面:

#!/usr/bin/ruby -w

class Box
  # constructor method
  def initialize(w,h)
   @width, @height = w, h
  end
  # define to_s method
  def to_s
   "(w:#@width,h:#@height)" # string formatting of the object.
  end
end

# create an object
box = Box.new(10, 20)

# to_s method will be called in reference of string automatically.
puts "String representation of box is : #{box}"

</div>

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

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

</div>

访问控制:

Ruby提供了三个级别的保护实例方法的级别:public, private 和 protected。 Ruby没有应用实例和类变量的任何访问控制权。

  1.     Public Methods: 任何人都可以被称为public方法。方法默认为公用初始化,这始终是 private 除外。 .
  2.     Private Methods: private方法不能被访问,或者甚至从类的外部浏览。只有类方法可以访问私有成员。
  3.     Protected Methods: 受保护的方法可以被调用,只能通过定义类及其子类的对象。访问保存在类内部。

以下是一个简单的例子来说明三个访问修饰符的语法:

#!/usr/bin/ruby -w

# define a class
class Box
  # constructor method
  def initialize(w,h)
   @width, @height = w, h
  end

  # instance method by default it is public
  def getArea
   getWidth() * getHeight
  end

  # define private accessor methods
  def getWidth
   @width
  end
  def getHeight
   @height
  end
  # make them private
  private :getWidth, :getHeight

  # instance method to print area
  def printArea
   @area = getWidth() * getHeight
   puts "Big box area is : #@area"
  end
  # make it protected
  protected :printArea
end

# create an object
box = Box.new(10, 20)

# call instance methods
a = box.getArea()
puts "Area of the box is : #{a}"

# try to call protected or methods
box.printArea()

</div>

当上面的代码被执行时,产生下面的结果。在这里,第一种方法被调用成功,但第二种方法给一个提示。

Area of the box is : 200
test.rb:42: protected method `printArea' called for #
<Box:0xb7f11280 @height=20, @wid
  


 

您可能想查找下面的文章:

  • ruby迭代map的简洁写法实现原理分析
  • Ruby中访问SQL Server数据库的配置实例
  • ruby声明式语法的实现例子
  • Ruby中的Range对象学习笔记
  • Ruby中的String对象学习笔记
  • Ruby的基本语法学习总结
  • Ruby中的方法(函数)学习总结
  • Ruby中的变量学习总结
  • Ruby数组(Array)学习笔记
  • Ruby和元编程之万物皆为对象

相关文章

  • Ruby实现的3种快速排序算法
  • Ruby实现插入排序算法及进阶的二路插入排序代码示例
  • Ruby使用REXML库来解析xml格式数据的方法
  • Ruby语言中的String深入理解
  • Ruby中操作文件的方法介绍
  • 几个Ruby小技巧分享
  • Ruby的面向对象方式编程学习杂记
  • 举例理解Ruby on Rails的页面缓存机制
  • 什么是ruby和Ruby概述
  • 浅谈Rails 4 中Strong Parameters机制

文章分类

  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell

最近更新的内容

    • 详解组合模式的结构及其在Ruby设计模式编程中的运用
    • Windows下安装配置Ruby的debug工具ruby-debug-base19
    • 初步讲解Ruby编程中的多线程
    • 优化Ruby脚本效率实例分享
    • Ruby类继承、抽象类、类拓展混入、代理类实例
    • Ruby中遍历目录的简洁方法
    • 编写Ruby脚本来对Twitter用户的数据进行深度挖掘
    • Ruby语法笔记
    • 简单对比分析Ruby on Rails 和 Laravel
    • 使用ruby部署工具mina快速部署nodejs应用教程

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有