首页 > 编程语言 >无涯教程-Ruby Class Case Study函数

无涯教程-Ruby Class Case Study函数

时间:2023-11-20 21:32:15浏览次数:40  
标签:Case Customer customers cust no Study 无涯 id name

对于您的案Example研究,您将创建一个名为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

The display_details method contains three puts statements, displaying the Customer ID, the Customer name, and the Customer address. The puts statement will display the text Customer id followed by the value of the variable @cust_id in a single line as follows −

puts "Customer id #@cust_id"

如果要在一行中显示变量的文本和值,则需要在puts语句中的变量名称前加上井号(#)。文本和变量以及井号(#)应该用双引号引起来。

The second method, total_no_of_customers, is a method that contains the class variable @@no_of_customers. The expression @@no_of_ customers+=1 adds 1 to the variable no_of_customers each time the method total_no_of_customers is called. In this way, you will always have the total number of customers in the class variable.

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

cust1=Customer.new("1", "John", "Wisdom Apartments, Ludhiya")
cust2=Customer.new("2", "Poul", "New Empire road, Khandala")

在这里,我们创建Customer类的两个对象cust1和cust2,并使用新方法传递必要的参数。调用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

# Create Objects
cust1=Customer.new("1", "John", "Wisdom Apartments, Ludhiya")
cust2=Customer.new("2", "Poul", "New Empire road, Khandala")

# Call Methods
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

参考链接

https://www.learnfk.com/ruby/ruby-class-case-study.html

标签:Case,Customer,customers,cust,no,Study,无涯,id,name
From: https://blog.51cto.com/u_14033984/8491454

相关文章

  • 无涯教程-Ruby - Ruby环境变量函数
    Ruby解释器使用以下环境变量来控制其行为。ENV对象包含所有当前环境变量集的列表。Sr.No.Variable&Remark1DLN_LIBRARY_PATH动态加载的模块的搜索路径。2HOME没有将任何参数传递给Dir::chdir时目录移动到。也由File::expand_path用来扩展"〜"。3LOGDIR没有......
  • 无涯教程-Ruby - Ruby命令行选项函数
    Ruby通常以以下方式从命令行运行-$ruby[options][.][programfile][arguments...]可以使用以下任何选项来调用解释器,以控制解释器的环境和行为。Sr.No.Option&Remark1-a与-n或-p一起使用以分割每行。检查-n和-p选项。2-c仅检查语法,而不执行程序。3......
  • 无涯教程-Ruby - 面向对象
    Ruby是一种纯粹的面向对象的语言,所有事物在Ruby中都是对象,Ruby中的每个值都是一个对象。本章将带您了解与面向对象的Ruby相关的所有主要函数。Ruby类定义类定义以关键字class开头,后跟classname,并以end分隔。如,无涯教程使用关键字class定义Box类,如下所示:classBox......
  • 无涯教程-Ruby - 文件IO
    Ruby提供了在Kernel模块中实现的与I/O相关的整套方法。所有I/O方法都是从IO类派生的。IO提供了所有基本方法,如read,write,gets,puts,readline,getc,和printf 。本章将介绍Ruby中可用的所有基本I/O函数。有关更多函数,请参阅RubyClassIO。Puts语句在前面的章节中,您已经为变量......
  • 无涯教程-Ruby - 迭代器
    迭代器不过是collections 集合支持的方法。存储一组数据成员的对象称为集合。在Ruby中,数组和哈希可以称为集合。迭代器一个接一个地返回集合的所有元素。无涯教程将在这里讨论两个迭代器,分别是each和collect。Each迭代器每个迭代器返回数组或哈希的所有元素。collecti......
  • 无涯教程-Ruby - 模块语句
    Module语法moduleIdentifierstatement1statement2...........end模块常量的命名与类常量一样,并带有大写字母开头。方法定义也看起来相似:模块方法的定义就像类方法一样。与类方法一样,您可以通过在模块名称前加上模块名称和句点来调用模块方法,并使用模块名称和......
  • 无涯教程-Ruby - 变量声明
    变量是存储位置,用于保存任何程序要使用的任何数据,Ruby支持五种类型的变量,本章介绍了这五种变量。Ruby全局变量全局变量以"$"开头。未初始化的全局变量的值为nil并使用-w选项生成警告。分配给全局变量会更改全局状态,不建议使用全局变量,它们使程序变得很难维护,下面是全局变量......
  • 无涯教程-Ruby - Blocks块
    您已经了解了Ruby是如何定义方法的,可以在其中放置大量语句,然后调用该方法。同样,Ruby也具有Block的概念。Block语法block_name{statement1statement2..........}在这里,您将学习使用简单的yield语句来调用块。您还将学习使用带参数的yield语句来调用块。Yie......
  • 无涯教程-Ruby - 方法声明
    Ruby方法与任何其他编程语言中的函数都非常相似。 方法名称应以小写字母开头,如果您以大写字母开头的方法名称,Ruby可能会认为它是一个常量,因此可能会错误地解析该调用。方法应该在调用它们之前定义,否则Ruby将为未定义的方法调用引发异常。语法defmethod_name[([arg[=def......
  • 无涯教程-Ruby - 注释符
    注释是Ruby代码中的注释行,在运行时会被忽略。一行注释以#字符开头,它们从#延伸到该行的末尾,如下所示-#!/usr/bin/ruby-w#Thisisasinglelinecomment.puts"Hello,Ruby!"执行后,上述程序会产生以下输出-Hello,Ruby!Ruby多行注释您可以使用=begin和=end语法注释多......