Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Ruby / Yukihiro Matsumoto_Programming Ruby.doc
Скачиваний:
122
Добавлен:
06.06.2015
Размер:
2.71 Mб
Скачать

Inheritance and Mixins

Some object-oriented languages (notably C++) support multiple inheritance, where a class can have more than one immediate parent, inheriting functionality from each. Although powerful, this technique can be dangerous, as the inheritance hierarchy can become ambiguous.

Other languages, such as Java, support single inheritance. Here, a class can have only one immediate parent. Although cleaner (and easier to implement), single inheritance also has drawbacks---in the real world things often inherit attributes from multiple sources (a ball is both a bouncing thingand aspherical thing, for example).

Ruby offers an interesting and powerful compromise, giving you the simplicity of single inheritance and the power of multiple inheritance. A Ruby class can have only one direct parent, and so Ruby is a single-inheritance language. However, Ruby classes can include the functionality of any number of mixins (a mixin is like a partial class definition). This provides a controlled multiple-inheritance-like capability with none of the drawbacks. We'll explore mixins more beginning on page 98.

So far in this chapter we've been looking at classes and their methods. Now it's time to move on to the objects, such as the instances of class Song.

Objects and Attributes

The Songobjects we've created so far have an internal state (such as the song title and artist). That state is private to those objects---no other object can access an object's instance variables. In general, this is a Good Thing. It means that the object is solely responsible for maintaining its own consistency.

However, an object that is totally secretive is pretty useless---you can create it, but then you can't do anything with it. You'll normally define methods that let you access and manipulate the state of an object, allowing the outside world to interact with the object. These externally visible facets of an object are called its attributes.

For our Songobjects, the first thing we may need is the ability to find out the title and artist (so we can display them while the song is playing) and the duration (so we can display some kind of progress bar).

class Song

  def name

    @name

  end

  def artist

    @artist

  end

  def duration

    @duration

  end

end

aSong = Song.new("Bicylops", "Fleck", 260)

aSong.artist

»

"Fleck"

aSong.name

»

"Bicylops"

aSong.duration

»

260

Here we've defined three accessor methods to return the values of the three instance attributes. Because this is such a common idiom, Ruby provides a convenient shortcut: attr_readercreates these accessor methods for you.

class Song

  attr_reader :name, :artist, :duration

end

aSong = Song.new("Bicylops", "Fleck", 260)

aSong.artist

»

"Fleck"

aSong.name

»

"Bicylops"

aSong.duration

»

260

This example has introduced something new. The construct :artistis an expression that returns aSymbolobject corresponding toartist. You can think of:artistas meaning thenameof the variableartist, while plainartistis thevalueof the variable. In this example, we named the accessor methodsname,artist, andduration. The corresponding instance variables,@name,@artist, and@duration, will be created automatically. These accessor methods are identical to the ones we wrote by hand earlier.

Соседние файлы в папке Ruby