简体   繁体   English

如何在 R Z531704A02607A1646EFCF4C1FAE1EEC6 中实现 OOPS class object

[英]How to implement a OOPS class object in R shiny?

I want to implement an OOPS concept similar to building a class object with private data members in R shiny application.我想实现一个 OOPS 概念,类似于在 R Z531704A41607A16ZEF6 应用程序中使用私有数据成员构建 class object。 How can I do that?我怎样才能做到这一点?

R is a functional language that uses concepts of OOPs. R 是一种使用 OOP 概念的函数式语言。 OOPs provides classes and objects as its key tools to reduce and manage the complexity of the program. OOP 提供类和对象作为降低和管理程序复杂性的关键工具。 An object is also called an instance of a class and the process of creating this object is called instantiation. object 也称为 class 的实例,创建此 object 的过程称为实例化。 There are two important classes in R which are as follows: R 中有两个重要的类如下:

S3 class S3 class

does not have a predefined definition and able to dispatch.没有预定义的定义并且能够调度。 In this class, the generic function makes a call to the method.在此 class 中,通用 function 调用该方法。 Easy implementation of S3 is possible because it differs from the traditional programming language Java, C++, and C# which implements Object Oriented message passing. Easy implementation of S3 is possible because it differs from the traditional programming language Java, C++, and C# which implements Object Oriented message passing.

In the following code a Student class is defined.在以下代码中,定义了 Student class。 Appropriate class name is given having attributes student's name and roll number.适当的 class 名称具有学生姓名和卷号属性。 Then the object of student class is created and invoked.然后创建并调用学生 class 的 object。

# List creation with its attributes name and roll no. 
a <- list(name = "Adam", Roll_No = 15 )   
  
# Defining a class "Student" 
class(a) <- "Student"  
  
# Creation of object 
a 

S4 Class S4 Class

S4 class has a predefined definition. S4 class 有一个预定义的定义。 It contains functions for defining methods and generics.它包含用于定义方法和 generics 的函数。 It makes multiple dispatches easy.它使多次调度变得容易。 This class contains auxiliary functions for defining methods and generics.这个 class 包含用于定义方法和 generics 的辅助函数。

# Function setClass() command used  
# to create S4 class containing list of slots. 
setClass("Student", slots=list(name="character",  
                               Roll_No="numeric")) 
  
# 'new' keyword used to create object of class 'Student'    
a <- new("Student", name="Adam", Roll_No=20)   
  
# Calling object 
a 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM