Ruby 简明教程
Ruby - Classes and Objects
Ruby 是一个完美的面向对象编程语言。面向对象编程语言具有以下特点 −
Ruby is a perfect Object Oriented Programming Language. The features of the object-oriented programming language include −
-
Data Encapsulation
-
Data Abstraction
-
Polymorphism
-
Inheritance
这些特点已在章节 Object Oriented Ruby 中进行讨论。
These features have been discussed in the chapter Object Oriented Ruby.
面向对象程序涉及类和对象。类是单个对象根据其创建的蓝图。在面向对象术语中,我们说你的自行车是自行车类对象的一个实例。
An object-oriented program involves classes and objects. A class is the blueprint from which individual objects are created. In object-oriented terms, we say that your bicycle is an instance of the class of objects known as bicycles.
以任何车辆为例。它包含轮子、马力和燃油或油箱容量。这些特性形成了类 Vehicle 的数据成员。你可以借助这些特性来区分一辆车与另一辆车。
Take the example of any vehicle. It comprises wheels, horsepower, and fuel or gas tank capacity. These characteristics form the data members of the class Vehicle. You can differentiate one vehicle from the other with the help of these characteristics.
车辆还具有一些功能,例如停车、行驶和加速。即使这些功能也形成了类 Vehicle 的数据成员。因此,你可以将类定义为特性和功能的组合。
A vehicle can also have certain functions, such as halting, driving, and speeding. Even these functions form the data members of the class Vehicle. You can, therefore, define a class as a combination of characteristics and functions.
类 Vehicle 可以定义为 −
A class Vehicle can be defined as −
Class Vehicle {
Number no_of_wheels
Number horsepower
Characters type_of_tank
Number Capacity
Function speeding {
}
Function driving {
}
Function halting {
}
}
通过为这些数据成员指定不同的值,可以形成车辆类的多个实例。例如,飞机有三个轮子、1000 马力、燃油箱类型和 100 升容积。同样的,汽车有四个轮子、200 马力、汽油箱类型和 25 升容积。
By assigning different values to these data members, you can form several instances of the class Vehicle. For example, an airplane has three wheels, horsepower of 1,000, fuel as the type of tank, and a capacity of 100 liters. In the same way, a car has four wheels, horsepower of 200, gas as the type of tank, and a capacity of 25 liters.
Defining a Class in Ruby
要使用 Ruby 实现面向对象编程,首先需要学习如何在 Ruby 中创建对象和类。
To implement object-oriented programming by using Ruby, you need to first learn how to create objects and classes in Ruby.
Ruby 中的类总是以关键字 class 开始,后跟类的名称。名称始终应以大写字母开头。Customer 类可以显示为 −
A class in Ruby always starts with the keyword class followed by the name of the class. The name should always be in initial capitals. The class Customer can be displayed as −
class Customer
end
使用关键字 end 终止一个类。类中的所有数据成员都位于类定义和 end 关键字之间。
You terminate a class by using the keyword end. All the data members in the class are between the class definition and the end keyword.
Variables in a Ruby Class
Ruby 提供四种类型的变量 −
Ruby provides four types of variables −
-
Local Variables − Local variables are the variables that are defined in a method. Local variables are not available outside the method. You will see more details about method in subsequent chapter. Local variables begin with a lowercase letter or _.
-
Instance Variables − Instance variables are available across methods for any particular instance or object. That means that instance variables change from object to object. Instance variables are preceded by the at sign (@) followed by the variable name.
-
Class Variables − Class variables are available across different objects. A class variable belongs to the class and is a characteristic of a class. They are preceded by the sign @@ and are followed by the variable name.
-
Global Variables − Class variables are not available across classes. If you want to have a single variable, which is available across classes, you need to define a global variable. The global variables are always preceded by the dollar sign ($).
Creating Objects in Ruby using new Method
对象是类的实例。你现在将学习如何在 Ruby 中创建类的对象。可以使用类的 new 方法在 Ruby 中创建对象。
Objects are instances of the class. You will now learn how to create objects of a class in Ruby. You can create objects in Ruby by using the method new of the class.
new 方法是一种独特的类型方法,它在 Ruby 库中预定义。new 方法属于类方法。
The method new is a unique type of method, which is predefined in the Ruby library. The new method belongs to the class methods.
以下是创建 Customer 类对象的两个对象 cust1 和 cust2 的示例 −
Here is the example to create two objects cust1 and cust2 of the class Customer −
cust1 = Customer. new
cust2 = Customer. new
此处,cust1 和 cust2 是两个对象的名字。在等于号 (=) 之后编写对象名称,然后是类名称。然后,跟随点运算符和 new 关键字。
Here, cust1 and cust2 are the names of two objects. You write the object name followed by the equal to sign (=) after which the class name will follow. Then, the dot operator and the keyword new will follow.
Custom Method to Create Ruby Objects
可以将参数传递给 new 方法,这些参数可用于初始化类变量。
You can pass parameters to method new and those parameters can be used to initialize class variables.
当计划用参数声明 new 方法时,需要在创建类时声明 initialize 方法。
When you plan to declare the new method with parameters, you need to declare the method initialize at the time of the class creation.
initialize 方法是一种特殊类型的方法,当使用参数调用类的 new 方法时会执行该方法。
The initialize method is a special type of method, which will be executed when the new method of the class is called with parameters.
以下是创建初始化方法的示例 −
Here is the example to create initialize method −
class Customer
@@no_of_customers = 0
def initialize(id, name, addr)
@cust_id = id
@cust_name = name
@cust_addr = addr
end
end
在此示例中,使用 id, name 和 addr 作为局部变量声明 initialize 方法。此处,def 和 end 用于定义 Ruby 方法 initialize。你将在后面的章节中了解有关方法的更多信息。
In this example, you declare the initialize method with id, name, and addr as local variables. Here, def and end are used to define a Ruby method initialize. You will learn more about methods in subsequent chapters.
在 initialize 方法中,您将这些局部变量传递到实例变量 @cust_id、@cust_name、@cust_addr 中。在此,局部变量包含传递到 new 方法的值。
In the initialize method, you pass on the values of these local variables to the instance variables @cust_id, @cust_name, and @cust_addr. Here local variables hold the values that are passed along with the new method.
现在,您可以按照如下方式来创建对象。
Now, you can create objects as follows −
cust1 = Customer.new("1", "John", "Wisdom Apartments, Ludhiya")
cust2 = Customer.new("2", "Poul", "New Empire road, Khandala")
Member Functions in Ruby Class
在 Ruby 中,函数称为方法。类中的每个方法以关键字 def 开始,后跟方法名称。
In Ruby, functions are called methods. Each method in a class starts with the keyword def followed by the method name.
方法名称始终首选 lowercase letters 。您使用关键字 end 在 Ruby 中结束一个方法。
The method name always preferred in lowercase letters. You end a method in Ruby by using the keyword end.
这里是一个定义 Ruby 方法的示例:
Here is the example to define a Ruby method −
class Sample
def function
statement 1
statement 2
end
end
在此,语句 1 和语句 2 是类 Sample 中方法函数主体的一部分。这些语句可以是任何有效的 Ruby 语句。例如,我们可以使用 puts 方法像下面这样打印 Hello Ruby:
Here, statement 1 and statement 2 are part of the body of the method function inside the class Sample. These statments could be any valid Ruby statement. For example we can put a method puts to print Hello Ruby as follows −
class Sample
def hello
puts "Hello Ruby!"
end
end
现在,在以下示例中,创建一个 Sample 类的对象,并调用 hello 方法,然后查看结果:
Now in the following example, create one object of Sample class and call hello method and see the result −
#!/usr/bin/ruby
class Sample
def hello
puts "Hello Ruby!"
end
end
# Now using above class to create objects
object = Sample. new
object.hello
这会产生以下结果 −
This will produce the following result −
Hello Ruby!