在R中继承类或乘法类?

时间:2022-08-30 14:18:48

There appears an interesting situation in R S3 objects because they do not inherits classes.

R S3对象中出现了一个有趣的情况,因为它们不继承类。

One can see that agnes.object (result of agnes{cluster} function ) has 2 different classes.

可以看出agnes.object(agnes {cluster}函数的结果)有2个不同的类。

library( cluster )
example( agnes )
class( agn1 )
[1] "agnes" "twins"

But meantime when you read agnes.object documentation there is a section INHERITANCE and there there is a sentence: 'The class "agnes" inherits from "twins".'

但与此同时,当您阅读agnes.object文档时,有一节“继承”,并且有一句话:“该类”agnes“继承自”双胞胎“。

What do you think about that? Is it really inheritance or just multiply classes :)?

你觉得怎么样?它真的是继承还是繁殖类:)?

1 个解决方案

#1


1  

S3 allows a linear hierarchy of classes. So

S3允许类的线性层次结构。所以

> class(agn1)
[1] "agnes" "twins"

says that agn1 is an instance of class agnes, which extends (inherits from) twins. Here we define generics and methods

说agn1是类agnes的一个实例,它扩展(继承自)双胞胎。在这里,我们定义泛型和方法

## S3 generics
foo <- function(x) UseMethod("foo")
bar <- function(x) UseMethod("bar")
baz <- function(x) UseMethod("baz")

## S3 methods -- append '.' and class name
foo.twins = function(x) "twins"
foo.agnes = function(x) "agnes"
bar.twins = function(x) "twins"
baz.agnes = function(x) "agnes"

and illustrate inheritance in dispatch (method selection).

并说明了调度中的继承(方法选择)。

> foo(agn1)
[1] "agnes"
> bar(agn1)
[1] "twins"
> baz(agn1)
[1] "agnes"

The S3 class system in R is instance-based, there is no formal class definition. The class inheritance is implied by the character vector. So an object with class "agnes" has no parent class, whereas a class c("agnes", "twins") defines a linear class hierarchy with parent "twins" (even if there is no object anywhere with class "twins" alone!). Because there is no formal class definition, two objects declaring themselves to be of the same class could have completely different structure, e.g.,

R中的S3类系统是基于实例的,没有正式的类定义。类继承由字符向量隐含。因此,具有类“agnes”的对象没有父类,而类c(“agnes”,“twins”)定义具有父“双胞胎”的线性类层次结构(即使没有任何对象单独使用类“双胞胎”) !)。因为没有正式的类定义,所以声明自己属于同一类的两个对象可能具有完全不同的结构,例如,

structure(list(), class="A")
structure(list(x=1, y=2, z=3), class="A")

can both legitimately claim to be of class "A"!

可以合法声称属于“A”级!

#1


1  

S3 allows a linear hierarchy of classes. So

S3允许类的线性层次结构。所以

> class(agn1)
[1] "agnes" "twins"

says that agn1 is an instance of class agnes, which extends (inherits from) twins. Here we define generics and methods

说agn1是类agnes的一个实例,它扩展(继承自)双胞胎。在这里,我们定义泛型和方法

## S3 generics
foo <- function(x) UseMethod("foo")
bar <- function(x) UseMethod("bar")
baz <- function(x) UseMethod("baz")

## S3 methods -- append '.' and class name
foo.twins = function(x) "twins"
foo.agnes = function(x) "agnes"
bar.twins = function(x) "twins"
baz.agnes = function(x) "agnes"

and illustrate inheritance in dispatch (method selection).

并说明了调度中的继承(方法选择)。

> foo(agn1)
[1] "agnes"
> bar(agn1)
[1] "twins"
> baz(agn1)
[1] "agnes"

The S3 class system in R is instance-based, there is no formal class definition. The class inheritance is implied by the character vector. So an object with class "agnes" has no parent class, whereas a class c("agnes", "twins") defines a linear class hierarchy with parent "twins" (even if there is no object anywhere with class "twins" alone!). Because there is no formal class definition, two objects declaring themselves to be of the same class could have completely different structure, e.g.,

R中的S3类系统是基于实例的,没有正式的类定义。类继承由字符向量隐含。因此,具有类“agnes”的对象没有父类,而类c(“agnes”,“twins”)定义具有父“双胞胎”的线性类层次结构(即使没有任何对象单独使用类“双胞胎”) !)。因为没有正式的类定义,所以声明自己属于同一类的两个对象可能具有完全不同的结构,例如,

structure(list(), class="A")
structure(list(x=1, y=2, z=3), class="A")

can both legitimately claim to be of class "A"!

可以合法声称属于“A”级!