【Ruby】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()

这将产生以下结果:

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.cnblogs.com/haizhibin1989/p/9160250.html

时间: 2024-07-28 19:44:53

【Ruby】Ruby 类案例的相关文章

雷林鹏分享:Ruby 类案例

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 en

【Ruby】类和对象

Ruby 类和对象 Ruby 是一种完美的面向对象编程语言.面向对象编程语言的特性包括: 数据封装 数据抽象 多态性 继承 这些特性将在 面向对象的 Ruby 中进行讨论. 一个面向对象的程序,涉及到的类和对象.类是个别对象创建的蓝图.在面向对象的术语中,您的自行车是自行车类的一个实例. 以车辆为例,它包括车轮(wheels).马力(horsepower).燃油或燃气罐容量(fuel or gas tank capacity).这些属性形成了车辆(Vehicle)类的数据成员.借助这些属性您能把

RUBY的类封装,继承,多态简单演示

class Person def initialize(name,age=18) @name=name @age=age @motherland="China" end def talk puts "my name is " [email protected]+",age is "+@age.to_s if @motherland == "China" puts "I am a China." else p

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

Ruby File 类和方法 File 表示一个连接到普通文件的 stdio 对象.open 为普通文件返回该类的一个实例. 类方法 序号方法 & 描述 1File::atime( path) 返回 path 的最后访问时间. 2File::basename( path[, suffix]) 返回 path 末尾的文件名.如果指定了 suffix,则它会从文件名末尾被删除. 例如:File.basename("/home/users/bin/ruby.exe") #=>

[Ruby] Ruby Variable Scope

Scope defines where in a program a variable is accessible. Ruby has four types of variable scope, local,global, instance and class. In addition, Ruby has one constant type. Each variable type is declared by using a special character at the start of t

SqlHelper类案例示范

using System; using System.Collections.Generic; using System.Text; using System.Data; using System.Data.SqlClient; namespace DAL { /// <summary> /// 数据层 - 数据库 操作类 /// </summary> internal class SqlHelper { //获得配置文件的连接字符串 public readonly static

Ruby on Rails Tutorial 第四章 Rails背后的Ruby 之 类

Ruby和其他面向对象的语言一样,使用类来组织方法,然后实例化类,创建对象.1.构造方法使用双引号是字符串的字面构造方法,也可以使用“具名构造方法”,即在类名上调用new方法 >> s="foobar" >> s.class => String >> s=String.new("foobar") >> s=="foobar" => true >> a=Array.new([1

关于Ruby常用语法案例累积

变量问题: 类变量和方法变量的区别是什么? 类变量:可以直接使用 方法变量:需要实例化后,才能使用该变量 案例一: class Person @@name = "Tom" @@names = "Toms" @@age = 10 def jek @jj = "jj" @@bb="wonter" end def Person.getName return @@name end def Person.getNames return

ruby File类

类方法 路径相关: File.basename(filename <, suffix>) -> string返回给定文件名 filename 的最后一部分.如果有 suffix 参数,且它出现在 filename 的末尾. 则它将被删除.通过使用 ".*" 可以去除任意扩展名 File.basename('/home/test/ruby.rb') #=> ruby.rb File.basename('/home/test/ruby.rb', '.rb') #=&