📜  R 编程中的类

📅  最后修改于: 2022-05-13 01:54:59.052000             🧑  作者: Mango

R 编程中的类

类和对象是面向对象编程的基本概念,围绕现实生活中的实体展开。 R 中的一切都是对象。对象只是具有一些方法和属性的数据结构。一个只是这些对象的蓝图或草图。它表示一种类型的所有对象共有的一组属性或方法。

与大多数其他编程语言不同,R 具有三类系统。这些是 S3、S4 和参考类。

S3 级

S3 是最简单但最流行的 OOP 系统,它缺乏正式的定义和结构。这种类型的对象可以通过添加一个属性来创建。下面是一个让事情更清楚的例子:

例子:

# create a list with required components
movieList <- list(name = "Iron man", leadActor = "Robert Downey Jr")
  
# give a name to your class
class(movieList) <- "movie"
  
movieList

输出:

$name
[1] "Iron man"
$leadActor
[1] "Robert Downey Jr"

在 S3 系统中,方法不属于该类。它们属于通用函数。这意味着我们不能在这里创建自己的方法,就像我们在 C++ 或Java等其他编程语言中所做的那样。但是我们可以定义泛型方法(例如 print)在应用于我们的对象时的作用。

print(movieList)

输出:

$name
[1] "Iron man"
$leadActor
[1] "Robert Downey Jr"

示例:创建用户定义的打印函数

# now let us write our method
print.movie <- function(obj)
{
    cat("The name of the movie is", obj$name,".\n")
    cat(obj$leadActor, "is the lead actor.\n")
}

输出:

The name of the movie is Iron man .
Robert Downey Jr is the lead actor.

S4班

其他语言(如 C++、 Java )的程序员可能会发现 S3 与他们对类的正常概念有很大不同,因为它缺乏类应该提供的结构。 S4 比 S3 略有改进,因为它的对象具有正确的定义,并且它为其对象提供了正确的结构。

例子:

library(methods)
  
# definition of S4 class
setClass("movies", slots=list(name="character", leadActor = "character"))
  
# creating an object using new() by passing class name and slot values
movieList <- new("movies", name="Iron man", leadActor = "Robert Downey Jr")
movieList

输出:

An object of class "movies"
Slot "name":
[1] "Iron man"
Slot "leadActor":
[1] "Robert Downey Jr"

如上例所示, setClass()用于定义类, new()用于创建对象。

S4 中方法的概念与S3 类似,即它们属于泛型函数。以下示例显示了如何创建方法:

movieList

输出:

An object of class "movies"
Slot "name":
[1] "Iron man"

Slot "leadActor":
[1] "Robert Downey Jr"

例子:

# using setMethod to set a method
setMethod("show", "movies",
function(object) 
{
    cat("The name of the movie is ", object@name, ".\n")
    cat(object@leadActor, "is the lead actor.\n")
}
)
movieList

输出:

[1] "show"
The name of the movie is  Iron man .
Robert Downey Jr is the lead actor.

参考类

参考类是对 S4 类的改进。这里的方法属于类。这些与其他语言的面向对象类非常相似。

定义参考类类似于定义 S4 类。我们使用setRefClass()代替setClass()和“fields”代替“slots”。

例子:

library(methods)
   
# setRefClass returns a generator 
movies <- setRefClass("movies", fields = list(name = "character",
                       leadActor = "character", rating = "numeric"))
  
#now we can use the generator to create objects
movieList <- movies(name = "Iron Man", 
                    leadActor = "Robert downey Jr", rating = 7)
movieList

输出:

Reference class object of class "movies"
Field "name":
[1] "Iron Man"
Field "leadActor":
[1] "Robert downey Jr"
Field "rating":
[1] 7

现在让我们通过一个示例来看看如何为我们的类添加一些方法。

例子

library(methods)
  
movies <- setRefClass("movies", fields = list(name = "character", 
                       leadActor = "character", rating = "numeric"), methods = list(
                       increment_rating = function()
                       {
                           rating <<- rating + 1
                       },
                       decrement_rating = function()
                       {
                           rating <<- rating - 1
                       }
                     ))
movieList <- movies(name = "Iron Man", 
                    leadActor = "Robert downey Jr", rating = 7)
  
# print the value of rating
movieList$rating
  
# increment and then print the rating
movieList$increment_rating()
movieList$rating
  
# decrement and print the rating
movieList$decrement_rating()
movieList$rating

输出:

[1] 7
[1] 8
[1] 7