网站建设报价单及项目收费明细表,在线搜索资源,做app模板下载网站,企业网站设计与规划论文前言
本章介绍Ruby的类和对象及类案例。
Ruby 类和对象
Ruby 是一种完美的面向对象编程语言。面向对象编程语言的特性包括#xff1a;
数据封装数据抽象多态性继承
这些特性将在 面向对象的 Ruby 中进行讨论。
一个面向对象的程序#xff0c;涉及到的类和对象。类是个别…前言
本章介绍Ruby的类和对象及类案例。
Ruby 类和对象
Ruby 是一种完美的面向对象编程语言。面向对象编程语言的特性包括
数据封装数据抽象多态性继承
这些特性将在 面向对象的 Ruby 中进行讨论。
一个面向对象的程序涉及到的类和对象。类是个别对象创建的蓝图。在面向对象的术语中您的自行车是自行车类的一个实例。
以车辆为例它包括车轮wheels、马力horsepower、燃油或燃气罐容量fuel or gas tank capacity。这些属性形成了车辆Vehicle类的数据成员。借助这些属性您能把一个车辆从其他车辆中区分出来。
车辆也能包含特定的函数比如暂停halting、驾驶driving、超速speeding。这些函数形成了车辆Vehicle类的数据成员。因此您可以定义类为属性和函数的组合。
类 Vehicle 的定义如下
实例
Class Vehicle
{Number no_of_wheelsNumber horsepowerCharacters type_of_tankNumber CapacityFunction speeding{}Function driving{}Function halting{}
}通过给这些数据成员分配不同的值您可以创建类 Vehicle 的不同实例。例如一架飞机有三个轮子马力 1,000燃油罐容量为 100 升。以同样的方式一辆汽车有四个轮子马力 200煤气罐容量为 25 升。
在 Ruby 中定义类
为了使用 Ruby 实现面向对象编程您需要先学习如何在 Ruby 中创建对象和类。
在 Ruby 中类总是以关键字 class 开始后跟类的名称。类名的首字母应该大写。类 Customer 如下所示
class Customer
end您可以使用关键字 end 终止一个类。类 中的所有数据成员都是介于类定义和 end 关键字之间。
Ruby 类中的变量
Ruby 提供了四种类型的变量
局部变量局部变量是在方法中定义的变量。局部变量在方法外是不可用的。在后续的章节中您将看到有关方法的更多细节。局部变量以小写字母或 _开始。实例变量实例变量可以跨任何特定的实例或对象中的方法使用。这意味着实例变量可以从对象到对象的改变。实例变量在变量名之前放置符号。类变量类变量可以跨不同的对象使用。类变量属于类且是类的一个属性。类变量在变量名之前放置符号。全局变量类变量不能跨类使用。如果您想要有一个可以跨类使用的变量您需要定义全局变量。全局变量总是以美元符号$开始。
实例 使用类变量 no_of_customers您可以判断被创建的对象数量这样可以确定客户数量。
class Customerno_of_customers0
end在 Ruby 中使用 new 方法创建对象
对象是类的实例。现在您将学习如何在 Ruby 中创建类的对象。在 Ruby 中您可以使用类的方法 new 创建对象。
方法 new 是一种独特的方法在 Ruby 库中预定义。new 方法属于类方法。
下面的实例创建了类 Customer 的两个对象 cust1 和 cust2
cust1 Customer. new
cust2 Customer. new在这里cust1 和 cust2 是两个对象的名称。对象名称后跟着等号等号后跟着类名然后是点运算符和关键字 new。
自定义方法来创建 Ruby 对象
您可以给方法 new 传递参数这些参数可用于初始化类变量。
当您想要声明带参数的 new 方法时您需要在创建类的同时声明方法 initialize。
initialize 方法是一种特殊类型的方法将在调用带参数的类的 new 方法时执行。
下面的实例创建了 initialize 方法
实例
class Customerno_of_customers0def initialize(id, name, addr)cust_ididcust_namenamecust_addraddrend
end在本实例中您可以声明带有 id、name、addr 作为局部变量的 initialize方法。在这里def 和 end 用于定义 Ruby 方法 initialize。在后续的章节中您将学习有关方法的更多细节。
在 initialize 方法中把这些局部变量的值传给实例变量 cust_id、cust_name 和 cust_addr。在这里局部变量的值是随着 new 方法进行传递的。
现在您可以创建对象如下所示
cust1Customer.new(1, John, Wisdom Apartments, Ludhiya)
cust2Customer.new(2, Poul, New Empire road, Khandala)Ruby 类中的成员函数
在 Ruby 中函数被称为方法。类中的每个方法是以关键字 def 开始后跟方法名。
方法名总是以小写字母开头。在 Ruby 中您可以使用关键字 end 来结束一个方法。
下面的实例定义了一个 Ruby 方法
class Sampledef functionstatement 1statement 2end
end在这里statement 1 和 statement 2 是类 Sample 内的方法 function 的主体的组成部分。这些语句可以是任何有效的 Ruby 语句。例如我们可以使用方法 puts 来输出 Hello Ruby如下所示
class Sampledef helloputs Hello Ruby!end
end下面的实例将创建类 Sample 的一个对象并调用 hello 方法
#!/usr/bin/rubyclass Sampledef helloputs Hello Ruby!end
end# 使用上面的类来创建对象
object Sample. new
object.hello这将会产生下面的结果
Hello Ruby!Ruby 类案例
下面将创建一个名为 Customer 的 Ruby 类声明两个方法
display_details该方法用于显示客户的详细信息。total_no_of_customers该方法用于显示在系统中创建的客户总数量。
实例
#!/usr/bin/rubyclass Customerno_of_customers0def initialize(id, name, addr)cust_ididcust_namenamecust_addraddrenddef display_details()puts Customer id #cust_idputs Customer name #cust_nameputs Customer address #cust_addrenddef total_no_of_customers()no_of_customers 1puts Total number of customers: #no_of_customersend
enddisplay_details 方法包含了三个 puts 语句显示了客户 ID、客户名字和客户地址。其中puts 语句
puts Customer id #cust_id将在一个单行上显示文本 Customer id 和变量 cust_id 的值。
当您想要在一个单行上显示实例变量的文本和值时您需要在 puts 语句的变量名前面放置符号#。文本和带有符号#的实例变量应使用双引号标记。
第二个方法total_no_of_customers包含了类变量 no_of_customers。表达式 no_of_ customers1 在每次调用方法 total_no_of_customers 时把变量 no_of_customers 加 1。通过这种方式您将得到类变量中的客户总数量。
现在创建两个客户如下所示
cust1Customer.new(1, John, Wisdom Apartments, Ludhiya)
cust2Customer.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/rubyclass Customerno_of_customers0def initialize(id, name, addr)cust_ididcust_namenamecust_addraddrenddef display_details()puts Customer id #cust_idputs Customer name #cust_nameputs Customer address #cust_addrenddef total_no_of_customers()no_of_customers 1puts Total number of customers: #no_of_customersend
end# 创建对象
cust1Customer.new(1, John, Wisdom Apartments, Ludhiya)
cust2Customer.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
文章转载自: http://www.morning.cklgf.cn.gov.cn.cklgf.cn http://www.morning.ktpzb.cn.gov.cn.ktpzb.cn http://www.morning.rhmk.cn.gov.cn.rhmk.cn http://www.morning.feites.com.gov.cn.feites.com http://www.morning.clpfd.cn.gov.cn.clpfd.cn http://www.morning.lkpzx.cn.gov.cn.lkpzx.cn http://www.morning.nzmw.cn.gov.cn.nzmw.cn http://www.morning.fwzjs.cn.gov.cn.fwzjs.cn http://www.morning.fqzz3.cn.gov.cn.fqzz3.cn http://www.morning.mlfgx.cn.gov.cn.mlfgx.cn http://www.morning.gnlyq.cn.gov.cn.gnlyq.cn http://www.morning.wxqmc.cn.gov.cn.wxqmc.cn http://www.morning.htpjl.cn.gov.cn.htpjl.cn http://www.morning.tpdg.cn.gov.cn.tpdg.cn http://www.morning.rdng.cn.gov.cn.rdng.cn http://www.morning.tpwrm.cn.gov.cn.tpwrm.cn http://www.morning.ljdd.cn.gov.cn.ljdd.cn http://www.morning.qcdhg.cn.gov.cn.qcdhg.cn http://www.morning.gwqkk.cn.gov.cn.gwqkk.cn http://www.morning.xsrnr.cn.gov.cn.xsrnr.cn http://www.morning.jyznn.cn.gov.cn.jyznn.cn http://www.morning.drytb.cn.gov.cn.drytb.cn http://www.morning.wgrm.cn.gov.cn.wgrm.cn http://www.morning.rlnm.cn.gov.cn.rlnm.cn http://www.morning.gcfrt.cn.gov.cn.gcfrt.cn http://www.morning.lfbsd.cn.gov.cn.lfbsd.cn http://www.morning.rtbj.cn.gov.cn.rtbj.cn http://www.morning.sfdsn.cn.gov.cn.sfdsn.cn http://www.morning.qgqck.cn.gov.cn.qgqck.cn http://www.morning.nhlnh.cn.gov.cn.nhlnh.cn http://www.morning.bzsqr.cn.gov.cn.bzsqr.cn http://www.morning.rnmdp.cn.gov.cn.rnmdp.cn http://www.morning.qfzjn.cn.gov.cn.qfzjn.cn http://www.morning.jsphr.cn.gov.cn.jsphr.cn http://www.morning.sbdqy.cn.gov.cn.sbdqy.cn http://www.morning.nnwnl.cn.gov.cn.nnwnl.cn http://www.morning.nlffl.cn.gov.cn.nlffl.cn http://www.morning.fwdln.cn.gov.cn.fwdln.cn http://www.morning.qlrtd.cn.gov.cn.qlrtd.cn http://www.morning.ykwbx.cn.gov.cn.ykwbx.cn http://www.morning.cknrs.cn.gov.cn.cknrs.cn http://www.morning.ltrms.cn.gov.cn.ltrms.cn http://www.morning.qbjrl.cn.gov.cn.qbjrl.cn http://www.morning.pbtdr.cn.gov.cn.pbtdr.cn http://www.morning.zsfooo.com.gov.cn.zsfooo.com http://www.morning.qbjrl.cn.gov.cn.qbjrl.cn http://www.morning.ghslr.cn.gov.cn.ghslr.cn http://www.morning.gidmag.com.gov.cn.gidmag.com http://www.morning.sjwzl.cn.gov.cn.sjwzl.cn http://www.morning.wqwbj.cn.gov.cn.wqwbj.cn http://www.morning.3ox8hs.cn.gov.cn.3ox8hs.cn http://www.morning.bqxxq.cn.gov.cn.bqxxq.cn http://www.morning.zbkdm.cn.gov.cn.zbkdm.cn http://www.morning.xrrjb.cn.gov.cn.xrrjb.cn http://www.morning.zbjfq.cn.gov.cn.zbjfq.cn http://www.morning.mhcft.cn.gov.cn.mhcft.cn http://www.morning.wtdyq.cn.gov.cn.wtdyq.cn http://www.morning.jqlx.cn.gov.cn.jqlx.cn http://www.morning.mrbzq.cn.gov.cn.mrbzq.cn http://www.morning.lxyyp.cn.gov.cn.lxyyp.cn http://www.morning.grbp.cn.gov.cn.grbp.cn http://www.morning.dmnqh.cn.gov.cn.dmnqh.cn http://www.morning.xdwcg.cn.gov.cn.xdwcg.cn http://www.morning.mjbkp.cn.gov.cn.mjbkp.cn http://www.morning.lhgqc.cn.gov.cn.lhgqc.cn http://www.morning.cfjyr.cn.gov.cn.cfjyr.cn http://www.morning.dmwck.cn.gov.cn.dmwck.cn http://www.morning.kqcqr.cn.gov.cn.kqcqr.cn http://www.morning.sfswj.cn.gov.cn.sfswj.cn http://www.morning.tlfzp.cn.gov.cn.tlfzp.cn http://www.morning.pkpqh.cn.gov.cn.pkpqh.cn http://www.morning.fbpyd.cn.gov.cn.fbpyd.cn http://www.morning.zxrtt.cn.gov.cn.zxrtt.cn http://www.morning.qhmgq.cn.gov.cn.qhmgq.cn http://www.morning.coatingonline.com.cn.gov.cn.coatingonline.com.cn http://www.morning.bxgpy.cn.gov.cn.bxgpy.cn http://www.morning.lkmks.cn.gov.cn.lkmks.cn http://www.morning.wqbbc.cn.gov.cn.wqbbc.cn http://www.morning.gqnll.cn.gov.cn.gqnll.cn http://www.morning.dtlnz.cn.gov.cn.dtlnz.cn