ruby 类 继承_Ruby类

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是一种纯粹的面向对象编程语言。 面向对象的程序由ClassesObjectsobject是一个实体,它充当数据的容器并控制对数据的访问。

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类来制作不同的形状,例如RectangleSquareCircle等等。 objectclass的实例。

Class Hierarchy in Ruby

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有很多类。 并非所有的类都包含在此层次结构中。 类基本对象是所有类的类。

Checking Super Class in 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的名称。

Creating a class in Ruby

在Ruby中创建对象 (Creating Objects in Ruby)

Objects in ruby are created using the method new. new method belongs to the class Class.

使用new方法创建Ruby中的Objectsnew方法属于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:

类的另一个示例:

Creating a objects in Ruby

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中,而变量firstmiddlelast是局部变量。 inspect方法提供有关类名和实例变量及其值列表的信息。

This is the output of the program:

这是程序的输出:

Creating a class & objects in Ruby

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.
Creating a class & objects in Ruby

The output of the program is :

该程序的输出为:

Creating a class & objects in Ruby

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时 。 在这里,我们实际上是在打印对象,让我们看一下更改后的输出:

Creating a class & objects in Ruby

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.

我们还可以编写方法以允许我们检查对象的单个字段或属性。

Accessing the attributes of a class

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 :

该程序的输出为:

Accessing the attributes of a class

Likewise, we can examine each attribute of the object. We can do this in another simple way.

同样,我们可以检查对象的每个属性。 我们可以用另一种简单的方式来做到这一点。

Accessing the attributes of a class

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:

该程序的输出为:

Accessing the attributes of a class

使属性可写 (Making the attributes Writable)

The attributes can be made writable / modified by the following statement:

可以通过以下语句使属性可写/修改

attr_writer :first, :middle, :last
Writing the attributes of a class

Here, we have modified the instance variables of the object obj1. The output of the program is :

在这里,我们修改了对象obj1的实例变量。 该程序的输出为:

Writing the attributes of a class

Here, you can see that the first, middle and last values were modified and printed.

在这里,您可以看到firstmiddlelast值已被修改和打印。

翻译自: https://www.studytonight.com/ruby/class-in-ruby

ruby 类 继承

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值