gpt4 book ai didi

r - 如何在 R 中创建虚拟引用类?

转载 作者:行者123 更新时间:2023-12-04 15:11:37 25 4
gpt4 key购买 nike

我在 help(ReferenceClasses) 中找不到关于虚拟/抽象类的太多信息- 任何人都可以提供一个创建一个的基本示例吗?此外,如何指定虚拟方法并强制子类必须实现它?

最佳答案

引用类是 S4 类。所以也许你应该看看 setClass 的帮助和 Classes :
这是一个虚拟示例:

# virtual Base Class
setRefClass(
Class="virtC",
fields=list(
.elt="ANY"
),
methods = list(
.method = function(){
print("Base virtual method is called")
}
),
contains=c("VIRTUAL")
)

## child 1
## field as numeric and base .method is used
setRefClass(
Class="childNum",
fields=list(
.elt="numeric"
),
contains=c("virtC")
)


## child 2
## field is char and .method is overwritten
setRefClass(
Class="childChar",
fields=list(
.elt="character"
),
methods = list(
.method = function(){print('child method is called')}
),
contains=c("virtC")
)
## new('virtA') ## thros an error can't isntantiate it
a = new("childChar",.elt="a")
b = new("childNum",.elt=1)

b$.method()
[1] "Base virtual method is called"

a$.method()
[1] "child method is called"

关于r - 如何在 R 中创建虚拟引用类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21142773/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com