gpt4 book ai didi

r - 动态扩展引用类时如何控制继承

转载 作者:行者123 更新时间:2023-12-01 05:37:34 26 4
gpt4 key购买 nike

在网络爬虫/网络爬虫设置中,我想动态扩展我的基本引用类 URL为了能够为各自的主机/域编写特定的方法。为了清楚起见,动态我的意思是“在遇到新域时自动生成类定义(例如类 URL_something.com 将从类 URL 继承)”。

很好用,唯一的问题是我的类(class) WebPage期望字段 url 的值成为类(class) URL .它将接受类 URL_something.com 的对象因为它继承自类 URL ,但实际上将对象转换为类 URL 的实例.所以我丢失了它实际上属于 URL_something.com 类的信息.

您知道如何防止丢失重要信息吗?

代码示例

setRefClass(Class="URL", fields=list(x="character"))
setRefClass(Class="WebPage", fields=list(url="URL"))

obj <- new("WebPage", url=new("URL", x="http://www.something.com/home/index.html"))
obj$url

# Method would recognize that there is no class 'URL_something.com'
# yet and thus create it:
setRefClass(Class="URL_something.com", contains="URL")

# Another method would take care of mapping field values to
# an instance of the new class:
> url.obj <- new("URL_something.com", x="http://www.something.com/home/index.html")
> inherits(url.obj, "URL")
[1] TRUE

> obj$url <- url.obj
> class(obj$url)
[1] "URL"
# So I lose the information that it was actually of class "URL_something.com"

最佳答案

接受 Martin 所说的(见上面的评论):R 2.14.0 修复了我上面描述的内容。

关于r - 动态扩展引用类时如何控制继承,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7812678/

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