Ruby 类案例

  下面将创建一个名为 Customer 的 Ruby 类,您将声明两个方法:

  display_details:该方法用于显示客户的详细信息。

  total_no_of_customers:该方法用于显示在系统中创建的客户总数量。

  #!/usr/bin/ruby

  class Customer

  @@no_of_customers=0

  def initialize(id, name, addr)

  @cust_id=id

  @cust_name=name

  @cust_addr=addr

  end

  def display_details()

  puts "Customer id #@cust_id"

  puts "Customer name #@cust_name"

  puts "Customer address #@cust_addr"

  end

  def total_no_of_customers()

  @@no_of_customers += 1

  puts "Total number of customers: #@@no_of_customers"

  end

  end

  display_details 方法包含了三个 puts 语句,显示了客户 ID、客户名字和客户地址。其中,puts 语句:

  puts "Customer id #@cust_id"

  将在一个单行上显示文本 Customer id,后跟变量 @cust_id 的值。

  当您想要在一个单行上显示实例变量的文本和值时,您需要在 puts 语句的变量名前面放置符号(#)。文本和带有符号(#)的实例变量应使用双引号标记。

  第二个方法,total_no_of_customers,包含了类变量 @@no_of_customers。表达式 @@no_of_ customers+=1 在每次调用方法 total_no_of_customers 时,把变量 no_of_customers 加 1。通过这种方式,您将得到类变量中的客户总数量。

  现在创建两个客户,如下所示:

  cust1=Customer.new("1", "John", "Wisdom Apartments, Ludhiya")

  cust2=Customer.new("2", "Poul", "New Empire road, Khandala")

  在这里,我们创建了 Customer 类的两个对象,cust1 和 cust2,并向 new 方法传递必要的参数。当 initialize 方法被调用时,对象的必要属性被初始化。

  一旦对象被创建,您需要使用两个对象来调用类的方法。如果您想要调用方法或任何数据成员,您可以编写代码,如下所示:

  cust1.display_details()

  cust1.total_no_of_customers()

  对象名称后总是跟着一个点号,接着是方法名称或数据成员。我们已经看到如何使用 cust1 对象调用两个方法。使用 cust2 对象,您也可以调用两个方法,如下所示:

  cust2.display_details()

  cust2.total_no_of_customers()

  保存并执行代码

  现在,把所有的源代码放在 main.rb 文件中,如下所示:

  #!/usr/bin/ruby

  class Customer

  @@no_of_customers=0

  def initialize(id, name, addr)

  @cust_id=id

  @cust_name=name

  @cust_addr=addr

  end

  def display_details()

  puts "Customer id #@cust_id"

  puts "Customer name #@cust_name"

  puts "Customer address #@cust_addr"

  end

  def total_no_of_customers()

  @@no_of_customers += 1

  puts "Total number of customers: #@@no_of_customers"

  end

  end

  # 创建对象

  cust1=Customer.new("1", "John", "Wisdom Apartments, Ludhiya")

  cust2=Customer.new("2", "Poul", "New Empire road, Khandala")

  # 调用方法

  cust1.display_details()

  cust1.total_no_of_customers()

  cust2.display_details()

  cust2.total_no_of_customers()

  接着,运行程序,如下所示:

  $ ruby main.rb

  这将产生以下结果:

  Customer id 1

  Customer name John

  Customer address Wisdom Apartments, Ludhiya

  Total number of customers: 1

  Customer id 2

  Customer name Poul

  Customer address New Empire road, Khandala

  Total number of customers: 2

  本文转载自:w3cschool(编辑:雷林鹏 来源:网络)

雷林鹏分享:Ruby 类案例的更多相关文章

  1. 雷林鹏分享:Ruby 循环

    Ruby 循环 Ruby 中的循环用于执行相同的代码块若干次.本章节将详细介绍 Ruby 支持的所有循环语句. Ruby while 语句 语法 while conditional [do] code ...

  2. 雷林鹏分享:Ruby 类和对象

    Ruby 类和对象 Ruby 是一种完美的面向对象编程语言.面向对象编程语言的特性包括: 数据封装 数据抽象 多态性 继承 这些特性将在 面向对象的 Ruby 中进行讨论. 一个面向对象的程序,涉及到 ...

  3. 雷林鹏分享:Ruby Dir 类和方法

    Ruby Dir 类和方法 Dir 是一个表示用于给出操作系统中目录中的文件名的目录流.Dir 类也拥有与目录相关的操作,比如通配符文件名匹配.改变工作目录等. 类方法 序号方法 & 描述 1 ...

  4. 雷林鹏分享:Ruby File 类和方法

    Ruby File 类和方法 File 表示一个连接到普通文件的 stdio 对象.open 为普通文件返回该类的一个实例. 类方法 序号方法 & 描述 1File::atime( path) ...

  5. 雷林鹏分享:Ruby 数据类型

    Ruby 数据类型 本章节我们将为大家介绍 Ruby 的基本数据类型. Ruby支持的数据类型包括基本的Number.String.Ranges.Symbols,以及true.false和nil这几个 ...

  6. 雷林鹏分享:Ruby 变量

    Ruby 变量 变量是持有可被任何程序使用的任何数据的存储位置. Ruby 支持五种类型的变量.您已经在前面的章节中大概了解了这些变量,本章节将为您详细讲解这五种类型的变量. Ruby 全局变量 全局 ...

  7. 雷林鹏分享:Ruby 运算符

    Ruby 运算符 Ruby 支持一套丰富的运算符.大多数运算符实际上是方法调用.例如,a + b 被解释为 a.+(b),其中指向变量 a 的 + 方法被调用,b 作为方法调用的参数. 对于每个运算符 ...

  8. 雷林鹏分享:Ruby 块

    Ruby 块 您已经知道 Ruby 如何定义方法以及您如何调用方法.类似地,Ruby 有一个块的概念. 块由大量的代码组成. 您需要给块取个名称. 块中的代码总是包含在大括号 {} 内. 块总是从与其 ...

  9. 雷林鹏分享:Ruby 方法

    Ruby 方法 Ruby 方法与其他编程语言中的函数类似.Ruby 方法用于捆绑一个或多个重复的语句到一个单元中. 方法名应以小写字母开头.如果您以大写字母作为方法名的开头,Ruby 可能会把它当作常 ...

随机推荐

  1. VC++文件操作之最全篇

    一.剖析VC中的文件操作 各种关于文件的操作在程序设计中是十分常见,如果能对其各种操作都了如指掌,就可以根据实际情况找到最佳的解决方案,从而在较短的时间内编写出高效的代码,因而熟练的掌握文件操作是十分 ...

  2. UVA 475

    /* 通过这题 学会了 两个词组 immediately to the left 是左邻的意思 immediately to the right 这个是右邻的意思 */ #include <io ...

  3. python yield yield from

    1.可迭代对象 具备可迭代的能力,即enumerable,在python中指的是可以通过for-in去逐个访问元素的一些对象,比如元组tuple,列表list,字符串string,文件对象file等. ...

  4. C/C++之static函数与普通函数

    全局变量(外部变量)的说明之前再冠以static 就构成了静态的全局变量.全局变量本身就是静态存储方式, 静态全局变量当然也是静态存储方式.这两者在存储方式上并无不同.这两者的区别虽在于非静态全局变量 ...

  5. 如何使用Unity制作虚拟导览(一)

    https://www.cnblogs.com/yangyisen/p/5108289.html Unity用来制作游戏已经是目前市场上的一个发展趋势,而且有越来越多的公司与开发者不断的加入,那么Un ...

  6. loj10009 P1717 钓鱼

    P1717 钓鱼 贪心+优先队列 先枚举最后走到哪个湖,然后用优先队列跑一遍贪心即可 #include<iostream> #include<cstdio> #include& ...

  7. nodejs 导出 exel文件 xlsx

    参考: https://www.npmjs.com/package/node-xlsx Building a xlsx import xlsx from 'node-xlsx'; // Or var ...

  8. CEF解决加载慢问题

    转载:http://blog.csdn.net/weolar/article/details/51994895 CEF加载慢的时候,加上以下代码,通过命令行的方式: CefRefPtr<CefC ...

  9. python面向对象总结!

    面向对象 Object Oriented Programming 基本单元:对象把数据和功能封装在里边,能实现很好的复用性,灵活性和扩展性. 面向对象的两个基本概念:类和对象 面向对象的基本要素:属性 ...

  10. windows下的gvim和emmet 下载和安装 + "omnifunc is not set" solution?

    注意几个地方: 引导键是ctrl-y, 其他就是实际的操作键了, 如: n下一个插入点, N是上一个插入点(不是p), ctrl-y + i是自动为图片添加宽度和高度尺寸, 要点是要把光标移动到 im ...