Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
R_inferno.pdf
Скачиваний:
6
Добавлен:
21.05.2015
Размер:
947.8 Кб
Скачать

7.2. S4

CIRCLE 7. TRIPPING ON OBJECT ORIENTATION

A list of all methods for median (in the current session) is found with:

methods(median)

and methods for the "factor" class are found with:

methods(class='factor')

7.1.3inheritance

Classes can inherit from other classes. For example:

> class(ordered(c(90, 90, 100, 110, 110))) [1] "ordered" "factor"

Class "ordered" inherits from class "factor". Ordered factors are factors, but not all factors are ordered. If there is a method for "ordered" for a speci c generic, then that method will be used when the argument is of class "ordered". However, if there is not a method for "ordered" but there is one for "factor", then the method for "factor" will be used.

Inheritance should be based on similarity of the structure of the objects, not similarity of the concepts for the objects. Matrices and data frames have similar concepts. Matrices are a specialization of data frames (all columns of the same type), so conceptually inheritance makes sense. However, matrices and data frames have completely di erent implementations, so inheritance makes no practical sense. The power of inheritance is the ability to (essentially) reuse code.

7.2S4 methods

S4 methods correspond to the green book.

S3 methods are simple and powerful, and a bit ad hoc. S4 methods remove the ad hoc|they are more strict and more general. The S4 methods technology is a sti er rope|when you hang yourself with it, it surely will not break. But that is basically the point of it|the programmer is restricted in order to make the results more dependable for the user. That's the plan anyway, and it often works.

7.2.1multiple dispatch

One feature of S4 methods that is missing from S3 methods (and many other object-oriented systems) is multiple dispatch. Suppose you have an object of class "foo" and an object of class "bar" and want to perform function fun on these objects. The result of

fun(foo, bar)

40

7.2. S4

CIRCLE 7. TRIPPING ON OBJECT ORIENTATION

Figure 7.1: The Simoniacs by Sandro Botticelli.

may or may not to be di erent from

fun(bar, foo)

If there are many classes or many arguments to the function that are sensitive to class, there can be big complications. S4 methods make this complicated situation relatively simple.

We saw that UseMethod creates an S3 generic function. S4 generic functions are created with standardGeneric.

7.2.2S4 structure

S4 is quite strict about what an object of a speci c class looks like. In contrast S3 methods allow you to merely add a class attribute to any object|as long as a method doesn't run into anything untoward, there is no penalty. A key advantage in strictly regulating the structure of objects in a particular class is that those objects can be used in C code (via the .Call function) without a copious amount of checking.

Along with the strictures on S4 objects comes some new vocabulary. The pieces (components) of the object are called slots. Slots are accessed by the 8 @ 8 operator. So if you see code like:

41

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]