ruby 类 继承
Ruby
is a pure Object Oriented Programming Language. An object-oriented program consists of Classes
and Objects
. An object
is an entity that serves as a container for data and also controls access to the data.
Ruby
是一种纯粹的面向对象编程语言。 面向对象的程序由Classes
和Objects
。 object
是一个实体,它充当数据的容器并控制对数据的访问。
A class
is like a blueprint that allows you to create objects and to create methods that relate to those objects. For example, you might use a Shape class to make different shapes like Rectangle, Square, Circle, and so on. An object
is an instance of a class
.
class
就像一个蓝图,它允许您创建对象以及创建与那些对象相关的方法。 例如,您可以使用Shape类来制作不同的形状,例如Rectangle , Square , Circle等等。 object
是class
的实例。

Class Hierarchy
类层次结构
There are many classes in Ruby
. Not all the classes are included in this Hierarchy. Class Basic Object is the superclass of all the classes.
Ruby
有很多类。 并非所有的类都包含在此层次结构中。 类基本对象是所有类的超类。

We have set the local variable val to be a string. When we check the class of val, by calling the .class method, it tells us that the class of val is String. Then, when we checked the superclass of String, by calling the .superclass method it tells us Object
, and so on.
我们将局部变量val设置为字符串。 当我们通过调用.class方法检查val的类时,它告诉我们val的类是String。 然后,当我们检查String的超类时,通过调用.superclass方法,它告诉我们Object
,依此类推。
The reason the last value is nil is because the class BasicObject has no superclass.
在Ruby中定义类 (Defining Class in Ruby)
A class in Ruby
starts with the keyword class
followed by the name of the class.
Ruby
的类以关键字class
开头,后跟class
的名称。

在Ruby中创建对象 (Creating Objects in Ruby)
Objects
in ruby are created using the method new
. new
method belongs to the class Class.
使用new
方法创建Ruby中的Objects
。 new
方法属于Class类 。
obj1 = Shape.new
This is the syntax for creating object. Here obj1 is the object name and Shape is the class name.
这是创建对象的语法。 这里obj1是对象名, Shape是类名。
Another example of Class:
类的另一个示例:

In the above program we have created a class named Name.
在上面的程序中,我们创建了一个名为Name的类。
The method initialize
is special type of method which is called by the new
method in the object creation. The initialize
method accepts three arguments which is stored in the instance variables @first, @middle and @last whereas the variables first, middle and last are local variables. The inspect
method gives the information about the class name and list of instance variables and their values.
方法initialize
是一种特殊的方法类型,它在对象创建中由new
方法调用。 initialize
方法接受三个参数,这些参数存储在实例变量@first , @middle和@last中,而变量first , middle和last是局部变量。 inspect
方法提供有关类名和实例变量及其值列表的信息。
This is the output of the program:
这是程序的输出:

Another example using to_s method:
使用to_s方法的另一个示例:
to_s (to string) method allows us to display the state of the object. State refers to all the data that is being stored in the class objects.

The output of the program is :
该程序的输出为:

When we change the last line of program to print obj1. Here we are literally printing the object, let’s see the output after this change :
当我们更改程序的最后一行以打印obj1时 。 在这里,我们实际上是在打印对象,让我们看一下更改后的输出:

So, when the object is printed, the to_s method
is called automatically. This method can be used to debug or test the code by displaying the state of the object at any particular time.
因此,在打印对象时,将自动调用to_s method
。 通过在任何特定时间显示对象的状态,此方法可用于调试或测试代码。
Ruby:访问类和对象属性 (Ruby: Accessing Class and Object Attributes)
We can also write methods to allow us to examine a single field or attribute of an object.
我们还可以编写方法以允许我们检查对象的单个字段或属性。

You have to define the method with the name of the attribute/field. Here we have created a method for the field first. The method is called using object name
.
您必须使用attribute / field的名称定义方法。 在这里,我们首先为该领域创建了一种方法。 使用object name
调用该方法。
The output of the program is :
该程序的输出为:

Likewise, we can examine each attribute of the object. We can do this in another simple way.
同样,我们可以检查对象的每个属性。 我们可以用另一种简单的方式来做到这一点。

The attributes can be examined by the following statement attr_Reader :first, :middle, :last. This method returns the values of the attributes of the object which is printed using print
method.
可以通过以下语句attr_Reader:first,:middle,:last检查属性。 此方法返回使用print
方法打印的对象的属性值。
The output of the program is:
该程序的输出为:

使属性可写 (Making the attributes Writable)
The attributes can be made writable / modified by the following statement:
可以通过以下语句使属性可写/修改 :
attr_writer :first, :middle, :last

Here, we have modified the instance variables of the object obj1. The output of the program is :
在这里,我们修改了对象obj1的实例变量。 该程序的输出为:

Here, you can see that the first, middle and last values were modified and printed.
在这里,您可以看到first , middle和last值已被修改和打印。
ruby 类 继承