제네릭과 메서드

R에서 S3와 R6로 배우는 Object-Oriented Programming

Richie Cotton

Data Evangelist at DataCamp

summary(c(TRUE, FALSE, NA, TRUE))
   Mode   FALSE    TRUE    NA's 
logical       1       2       1
summary(rgamma(1000, 1))
    Min.  1st Qu.   Median     Mean  3rd Qu.     Max. 
0.000354 0.276500 0.690300 1.020000 1.384000 9.664000
R에서 S3와 R6로 배우는 Object-Oriented Programming

 

 

 

함수 오버로딩 = 입력에 따라

                                                동작이 달라짐

R에서 S3와 R6로 배우는 Object-Oriented Programming

ch2_1-generics-and-methods.007.png

R에서 S3와 R6로 배우는 Object-Oriented Programming

ch2_1-generics-and-methods.008.png

R에서 S3와 R6로 배우는 Object-Oriented Programming

ch2_1-generics-and-methods.009.png

R에서 S3와 R6로 배우는 Object-Oriented Programming

ch2_1-generics-and-methods.010.png

R에서 S3와 R6로 배우는 Object-Oriented Programming
print
function (x, ...) 
UseMethod("print")
<bytecode: 0x1062f0870>
<environment: namespace:base>
R에서 S3와 R6로 배우는 Object-Oriented Programming

메서드 이름: generic.class

  • print.Date
  • summary.factor
  • unique.array
R에서 S3와 R6로 배우는 Object-Oriented Programming

메서드 시그니처는 제네릭 시그니처 포함

args(print)
function (x, ...) 
NULL
args(print.Date)
function (x, max = NULL, ...) 
NULL
R에서 S3와 R6로 배우는 Object-Oriented Programming

 

 

 

메서드 간 인수 전달: ...

제네릭과 메서드 모두에 포함

R에서 S3와 R6로 배우는 Object-Oriented Programming
print.function
function (x, useSource = TRUE, ...) 
.Internal(print.function(x, useSource, ...))
R에서 S3와 R6로 배우는 Object-Oriented Programming
print.Date
function (x, max = NULL, ...) 
{
    if (is.null(max)) 
        max <- getOption("max.print", 9999L)
    if (max < length(x)) {
        print(format(x[seq_len(max)]), max = max, ...)
        cat(" [ reached getOption(\"max.print\") -- omitted", 
            length(x) - max, "entries ]\n")
    }
    else print(format(x), max = max, ...)
    invisible(x)
}
R에서 S3와 R6로 배우는 Object-Oriented Programming

 

 

ch2_1-generics-and-methods.030.png

R에서 S3와 R6로 배우는 Object-Oriented Programming

ch2_1-generics-and-methods.031.png

R에서 S3와 R6로 배우는 Object-Oriented Programming

ch2_1-generics-and-methods.032.png

R에서 S3와 R6로 배우는 Object-Oriented Programming

요약

  • 함수는 제네릭 + 메서드로 분리
  • 메서드 이름은 generic.class
  • 메서드 인수는 제네릭 인수 포함
  • ... 인수 포함
  • lower_snake_case 또는 lowerCamelCase 사용
R에서 S3와 R6로 배우는 Object-Oriented Programming

연습해 봅시다!

R에서 S3와 R6로 배우는 Object-Oriented Programming

Preparing Video For Download...