2

我想弄清楚为什么下面的代码会引发错误:

Error in .local(.Object, ...) : argument "data" is missing, with no default

问题代码:

setClass("A", representation(a="numeric"), "VIRTUAL")
setClass("B", representation(b="numeric"), contains="A")

setMethod("initialize", "A", function(.Object, data){
  .Object@a <- data[1]
})

setMethod("initialize", "B", function(.Object, data){
  .Object@b <- data[2]
  callNextMethod()
})

data <- 1:2

new("B", data)

谢谢您的帮助!

4

2 回答 2

4

也许这就是你要找的?

setClass("A", representation(a="numeric"), "VIRTUAL")
setClass("B", representation(b="numeric"), contains="A")

setMethod("initialize", "A", function(.Object, data){
  .Object@a <- data[1]
  .Object
})

setMethod("initialize", "B", function(.Object, data){
  .Object@b <- data[2]
  .Object <- callNextMethod(.Object, data)
  .Object
})

data <- 1:2

new("B", data)
于 2013-08-08T12:50:44.237 回答
0

我在这个线程中找到了帮助:Stack Overflow - Inheritance in R

请参阅下面的改编代码示例:

setClass("A", representation(a="numeric"), "VIRTUAL")
setClass("B", representation(b="numeric"), contains="A")

setMethod("initialize", "A", function(.Object,..., a=numeric()){
  .Object@a <- data[1]
  callNextMethod(.Object, ..., a=a)
})

setMethod("initialize", "B", function(.Object,..., b=numeric()){
  .Object@b <- data[2]
  callNextMethod(.Object, ..., b=b)
})

data <- as.numeric(1:2)

new("B",a=data[1],b=data[2])

> An object of class "B"
> Slot "b":
> [1] 2

> Slot "a":
> [1] 1
于 2013-08-12T14:39:12.713 回答