Classes

The real world is filled by objects, and we can classify them.

现实世界中充满了对象,并且我们还可以给它们分类。

For example, a very small child is likely to say “bow-wow” when seeing a dog, regardless of the breed; we naturally see the world in terms of these categories.

举个例子,当一个小孩子看到了一条狗,他可能会说“汪汪”,而不管这条狗是什么品种。我们自然地会从这些类别中看到世界。

In OO programming terminology, a category of objects like “dog” is called a class, and some specific object belonging to a class is called an instance of that class.

OO编程的术语中,像“狗”这样的一类对象,被称为,属于的某些特定对象称为该的一个实例

Generally, to make an object in ruby or any other OO language, first one defines the characteristics of a class, then creates an instance. To illustrate the process, let’s first define a simple Dog class.

通常,在Ruby或任何其他OO语言中,想要创造一个对象,第一步是定义一个的特征,然后再创建一个实例。为了阐述这个过程,让我们先定义一个简单的Dog类。

  1. ruby> class Dog
  2. | def speak
  3. | puts "Bow Wow"
  4. | end
  5. | end
  6. nil

In ruby, a class definition is a region of code between the keywords class and end.

Ruby中,类定义是关键字classend之间的代码区域。

A def inside this region begins the definition of a method of the class, which as we discussed in the previous chapter, corresponds to some specific behavior for objects of that class.

区域内的def是定义类方法的开始,正如我们在前一章中所讨论的,类方法对应于该类对象的某些特定行为。

Now that we have defined a Dog class, we can use it to make a dog:

现在我们已经定义了一个Dog类,我们可以用它来生成一条狗:

  1. ruby> pochi = Dog.new
  2. #<Dog:0xbcb90>

We have made a new instance of the class Dog, and have given it the name pochi.

我们生成了类Dog的一个新实例,并且给它取名为pochi

The new method of any class makes a new instance. Because pochi is a Dog according to our class definition, it has whatever properties we decided a Dog should have. Since our idea of Dog-ness was very simple, there is just one trick we can ask pochi to do.

任何类的new方法都是生成一个新实例。因为pochi是根据我们的类定义而来的一个Dog,它有任何我们认为Dog应该拥有的属性。因为我们对Dog的想法非常简单,所以我们能让pochi做的只有一个小把戏。

  1. ruby> pochi.speak
  2. Bow Wow
  3. nil

Making a new instance of a class is sometimes called instantiating that class. We need to have a dog before we can experience the pleasure of its conversation; we can’t merely ask the Dog class to bark for us.

生成一个类的新实例有时被称为实例化该类。在我们能体会到狗的对话的乐趣之前,我们需要有一条狗才行;我们不能只是要求Dog类为我们吠叫。

  1. ruby> Dog.speak
  2. ERR: (eval):1: undefined method `speak' for Dog:class

It makes no more sense than trying to eat the concept of a sandwich.

吃三明治的概念是没有意义的。

On the other hand, if we want to hear the sound of a dog without getting emotionally attached, we can create (instantiate) an ephemeral, temporary dog, and coax a little noise out of it before it disappears.

换句话说,如果我们想要听到狗的叫声而无需感情上的依恋,我们可以生成(实例化)一条短暂的、临时的狗,在它消失之前,让它发出一点声音。

  1. ruby> (Dog.new).speak # 或者更常见的, Dog.new.speak
  2. Bow Wow
  3. nil

“Wait,” you say, “what’s all this about the poor fellow disappearing afterwards?”

“等等”,你说道:“这个可怜的家伙到底是怎么消失的呢?”

It’s true: if we don’t bother to give it a name (as we did for pochi), ruby’s automatic garbage collection decides it is an unwanted stray dog, and mercilessly disposes of it. Really it’s okay, you know, because we can make all the dogs we want.

事实是:如果我们不费心给它取一个名字(就像我们给它取名pochi),Ruby的自动垃圾回收便判定它是一条不再需要的流浪狗,并且残忍地处理它。真的这没问题,你知道的,我们可以制造所有我们想要的狗。

上一章 方法 下一章 继承