class                  package:base                  R Documentation

_O_b_j_e_c_t _C_l_a_s_s_e_s

_D_e_s_c_r_i_p_t_i_o_n:

     R possesses a simple generic function mechanism which can be used
     for an object-oriented style of programming.  Method despatch
     takes place based on the class of the first argument to the
     generic function.

_U_s_a_g_e:

     class(x)
     class(x) <- names
     unclass(x)
     inherits(x, what, which = FALSE)

_A_r_g_u_m_e_n_t_s:

       x: an objects

    what: a character vector naming classes.

   which: logical affecting return value: see Details.

_D_e_t_a_i_l_s:

     An R ``object'' is a data object which has a `class' attribute. A
     class attribute is a character vector giving the names of the
     classes which the object ``inherits'' from.  When a generic
     function `fun' is applied to an object with class attribute
     `c("first", "second")', the system searches for a function called
     `fun.first' and, if it finds it, applies it to the object.  If no
     such function is found, a function called `fun.second' is tried.
     If no class name produces a suitable function, the function
     `fun.default' is used.

     The function `class' prints the vector of names of classes an
     object inherits from.  Correspondingly, `class<-' sets the classes
     an object inherits from.

     `unclass' returns (a copy of) its argument with its class
     information removed.

     `inherits' indicates whether its first argument inherits from any
     of the classes specified in the `what' argument.  If `which' is
     `TRUE' then an integer vector of the same length as `what' is
     returned. Each element indicates the position in the `class(x)'
     matched by the element of `what'; zero indicates no match. If
     `which' is `FALSE' then `TRUE' is  returned by `inherits' if any
     of the names in `what' match with any `class'.

_S_e_e _A_l_s_o:

     `UseMethod', `NextMethod'.

_E_x_a_m_p_l_e_s:

       x <- 10
       inherits(x, "a") #FALSE
       class(x)<-c("a", "b")
       inherits(x,"a") #TRUE
       inherits(x, "a", TRUE) # 1
       inherits(x, c("a", "b", "c"), TRUE) # 1 2 0

