如何在 R6Class 的私有方法中访问 public 个成员?
How to access public members in private method in R6Class?
library(R6)
Person<-R6Class("Person",
public=list(
name=NULL,
age=NULL,
initialize=function(name,age){
self$name<-name
self$age<-age
},
GrowUP1=function(){
self$publicGrow()
},
publicGrow=function(){
self$age<-self$age+1
},
GrowUP2=function(){
self$privateGrow()
}
),
private=list(
privateGrow=function(){
self$age<-self$age+1
}
)
)
Person<-Person$new('Tom',20)
Person$age
Person$GrowUP1()
Person$age
Person$GrowUP2()
这是我的示例代码。
我运行最后一个代码Person$GrowUP2()
。
但是,我收到错误 Error in Person$GrowUP2() : attempt to apply non-function
不知道为什么这段代码不是running.I想用私有方法把publicmember.How修改成
调用私有函数使用private
:
GrowUP2=function(){
private$privateGrow()
}
Person<-Person$new('Tom',20)
Person$GrowUP1()
Person$GrowUP2()
Person$age
[1] 22
library(R6)
Person<-R6Class("Person",
public=list(
name=NULL,
age=NULL,
initialize=function(name,age){
self$name<-name
self$age<-age
},
GrowUP1=function(){
self$publicGrow()
},
publicGrow=function(){
self$age<-self$age+1
},
GrowUP2=function(){
self$privateGrow()
}
),
private=list(
privateGrow=function(){
self$age<-self$age+1
}
)
)
Person<-Person$new('Tom',20)
Person$age
Person$GrowUP1()
Person$age
Person$GrowUP2()
这是我的示例代码。
我运行最后一个代码Person$GrowUP2()
。
但是,我收到错误 Error in Person$GrowUP2() : attempt to apply non-function
不知道为什么这段代码不是running.I想用私有方法把publicmember.How修改成
调用私有函数使用private
:
GrowUP2=function(){
private$privateGrow()
}
Person<-Person$new('Tom',20)
Person$GrowUP1()
Person$GrowUP2()
Person$age
[1] 22