R--S4 Class

S4Class

泛型函數(shù)

#需要新的方式表示數(shù)據(jù),或者需要新函數(shù)根據(jù)不同參數(shù)類型做出不同反應
#R的OOP是基于泛型函數(shù)的
#創(chuàng)建泛型函數(shù)UseMethod()
whoAmI <- function(x,...) UseMethod("whoAmI")
whoAmI.foo <- function(x) print("I am a foo")
whoAmI.bar <- function(x) print("I am a bar")
whoAmI.default <- function(x) print("I don't know who I am")
#用class()函數(shù)查看對象所屬的類,每個對象都屬于0或1或多個類
#使用attr()函數(shù)給a指定類屬性,attr(x,"dim")維度屬性
a <- 1:10
attr(a,"class") <- "foo" 
#a就相當于foo類的一個對象
whoAmI(a)
b <- 2:10
attr(b,"class") <- c("baz","bam","bar")
whoAmI(b)
#使用methods()函數(shù)來找到給定泛型函數(shù)的所有實現(xiàn)方法
methods(whoAmI)

#若實例屬于多個在泛型函數(shù)中定義的類,按第一個類處理
meth1 <- function(x) UseMethod("meth1")
meth1.Mom <- function(x) print("Mom's meth1")
meth1.Dad <- function(x) print("Dad's meth1")
meth2 <- function(x) UseMethod("meth2")
meth2.Dad <- function(x) print("Dad's meth2")
a <- 1:10
attr(a,"class") <- c("Mom","Dad")
meth1(a)
meth2(a)
#泛型函數(shù)將不同的方法聚集到一起,
#由R根據(jù)函數(shù)的對象類型來決定選擇執(zhí)行那個方法```

#類

setClass(Class,representation(),prototype = ,contains = )

Class為字符串,表示類的名稱

representation為建立成員變量與其明確的類型

numeric,character,logical

prototype為接口提供默認的數(shù)據(jù)對象

contains該類所擴展的類,所有擴展的類的新實例都會繼承其所有父類的接口

setClass("foo",representation(a="character",b="numeric"))
setClass("bar",representation(c="numeric",d="numeric"))

繼承foo和bar類

setClass("baz",contains = c("foo","bar"))
getClass("baz")

使用new()為此類創(chuàng)建一個實例,第一個參數(shù)是類的字符串

x <- new("baz",a="xxx",b=5,c=3,d=9)

使用@操作符訪問成員變量,寫入或者讀取

x@a <- "xyz"

或者使用slot()函數(shù)

slot(x,"a") <- "i love you"```

虛類

#虛類是不會用來生成實例的類
#它們用來將擁有不同的representation的類(它們不能互相繼承,如foo和bar)
#連接起來,通過虛類為這些不同的representation提供相似的功能
#建立一個虛類,用其他類來擴展它
#一個樹狀圖的表示
setClass("dendNode")
setClass("dnode",representation(left="dendNode",right="dendNode",
                                height="numeric"),contains = "dendNode")
setClass("tnode",representation(height="numeric",value="numeric",
                                label="character"),contains = "dendNode")```

#初始化和原型

控制類的實例生成對象的成員變量的初始值

setClass("xx",representation(a="numeric",b="character"),
prototype(a=3,b="hi there"))
new("xx")

或者是給該類指定一個Initialize方法,用該方法為類的實例賦值

帶‘initialize’標記方法傳入?yún)?shù)為.Object

并且在initialize方法中返回該對象

setMethod("initialize","xx",function(.Object,b)
{
.Object@b <- b
.Object@a <- nchar(b)
.Object
})
new("xx",b="yowser")```

泛型函數(shù)與方法

#泛型函數(shù)實際上是一個分派機制,根據(jù)不同的輸入?yún)?shù)決定什么樣的方法被執(zhí)行
#方法是特殊的函數(shù),根據(jù)特定的輸入對象執(zhí)行需要執(zhí)行的任務
whatIs <- function(object) data.class(object)
whatIs(1:10)
whatIs1 <- function(object) cat("類:",data.class(object),"\n長度:",
                                length(object),"\n")
whatIs1(1:10)
#對于函數(shù)與矩陣單獨建立處理函數(shù)
whatIs1.function <- function(object){
  cat("類:",data.class(object),"\n")
}
#如果whatIs1函數(shù)定義為UseMethod()就可以真正的形成不用指定類的泛型函數(shù)
whatIs1.function(whatIs)
whatIs1.matrix <- function(object){
  cat("類:",data.class(object),"\n",nrow(object),"行",ncol(object),"列\(zhòng)n")
}
A <- matrix(1:6,nrow=3)
whatIs1.matrix(A)```

#定義方法

使用統(tǒng)一的函數(shù)名來處理,根據(jù)輸入?yún)?shù)的類型決定使用哪個函數(shù)

S4類中使用setMethod()

第一個參數(shù)設定定義給定類的泛型函數(shù)名

第二個參數(shù)是類的名稱,稱為簽名,第三個就是要處理的函數(shù)

setMethod("whatIs","function",whatIs.function)
setMethod("whatIs","matrix",whatIs1.matrix)

告訴setMethod對什么泛型函數(shù)指定方法,執(zhí)行該方法對應的參數(shù)類型,執(zhí)行的方法

whatIs(A)
whatIs(whatIs)```

訪問子函數(shù)

#@來訪問接口,實際中使用子函數(shù)訪問接口,成員變量
setClass("foo",representation(abc="ANY"))
#is there a function named a,and if so,is it a generic(類)?
if(!isGeneric("a")){
  if(is.function("a")) fun <- a
  #dispatches a method from the current function call for the generic function a
  else fun <- function(object) standardGeneric("a")#泛型函數(shù)
  #if there is already a non-generic function of this name
  #it will be used to define the generic,and the current function will
  #become the default method for the generic
  setGeneric("a",fun)
}
#定義一個泛型函數(shù)的給定類的方法
setMethod("a","foo",function(object) object@abc)
b <- new("foo",abc=10)
a(b)```
最后編輯于
?著作權歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

  • 前言 把《C++ Primer》[https://book.douban.com/subject/25708312...
    尤汐Yogy閱讀 9,688評論 1 51
  • importUIKit classViewController:UITabBarController{ enumD...
    明哥_Young閱讀 4,202評論 1 10
  • 重新系統(tǒng)學習下C++;但是還是少了好多知識點;socket;unix;stl;boost等; C++ 教程 | 菜...
    kakukeme閱讀 20,462評論 0 50
  • 剛開始參加時候,擔心自己沒時間去讀書,怕自己做不好,一轉(zhuǎn)眼21.天結(jié)束了。每天的一問堅持去完成,還偶爾參加社群...
    流年七里香農(nóng)莊閱讀 292評論 0 0
  • 今天是二十一天寫作訓練營第六期畢業(yè)典禮的日子,我的內(nèi)心涌起一陣力量。 首先,我是汗顏的。聽了優(yōu)秀學員的分享,驚覺離...
    千目_閱讀 262評論 0 0

友情鏈接更多精彩內(nèi)容